Rails pass parameter from input through button_to

28 views
Skip to first unread message

Taras Matsyk

unread,
Jul 28, 2015, 4:49:37 PM7/28/15
to rubyonra...@googlegroups.com
Hi everyone,

A short description of the issue. I have a one page app which displays a
list of items people can vote for. The tricky part is that some users
can add more than 1 vote because of their status. Each item is displayed
through @item.each loop with its picture, amount of votes and button
"Vote".

<% @items.each do |item| %>
<div class="itemContainer">
<h4 style="text-align:center"> <%= product.name %> </h4>
<%= image_tag(item.photo_url) %>
<%= label_tag("price: #{item.votes_left}") %>
<%= label_tag("left: #{item.votes}") %>
<%= number_field_tag 'quantity', nil, min: 1, max: 10 %>
<button class="btn btn-default"> Vote </button>
</div>
<% end %>

What I am interested in is in finding a way to send "number_field_tag
'quantity'" value in request by clicking "Vote" button.


Just a note, is there any default Rails way to do that so I do not have
to use JavaScript?

--
Posted via http://www.ruby-forum.com/.

Colin Law

unread,
Jul 28, 2015, 5:26:37 PM7/28/15
to rubyonra...@googlegroups.com
On 28 July 2015 at 21:48, Taras Matsyk <li...@ruby-forum.com> wrote:
> Hi everyone,
>
> A short description of the issue. I have a one page app which displays a
> list of items people can vote for. The tricky part is that some users
> can add more than 1 vote because of their status. Each item is displayed
> through @item.each loop with its picture, amount of votes and button
> "Vote".
>
> <% @items.each do |item| %>
> <div class="itemContainer">
> <h4 style="text-align:center"> <%= product.name %> </h4>
> <%= image_tag(item.photo_url) %>
> <%= label_tag("price: #{item.votes_left}") %>
> <%= label_tag("left: #{item.votes}") %>
> <%= number_field_tag 'quantity', nil, min: 1, max: 10 %>
> <button class="btn btn-default"> Vote </button>
> </div>
> <% end %>
>
> What I am interested in is in finding a way to send "number_field_tag
> 'quantity'" value in request by clicking "Vote" button.

You can use a form. That is what forms are for.

Colin

Taras Matsyk

unread,
Jul 29, 2015, 2:25:14 PM7/29/15
to rubyonra...@googlegroups.com
Thanks, I though that logically forms should not be used to display a
list of tens items with different properties.

Will follow your advice, @Colin

Colin Law

unread,
Jul 29, 2015, 4:13:18 PM7/29/15
to rubyonra...@googlegroups.com
On 29 July 2015 at 19:23, Taras Matsyk <li...@ruby-forum.com> wrote:
> Thanks, I though that logically forms should not be used to display a
> list of tens items with different properties.
>
> Will follow your advice, @Colin

Glad to be of help, there is no reason why you should not have a
number of small forms on a page.
In future though please remember to quote the previous message when
replying, I had to look back in my emails to find your original
message in order to find what you were referring to. Remember this is
a mailing list not a forum, though you may be using it via a
forum-like interface.

Thanks

Colin

Taras Matsyk

unread,
Jul 31, 2015, 4:42:45 PM7/31/15
to rubyonra...@googlegroups.com
Colin Law wrote in post #1176976:
@Colin, thanks. Now I know one more trick and the reason to use it. Will
be quoting everything I can :D
Reply all
Reply to author
Forward
0 new messages