Ruby-on-rails – Rails link_to or button_to post request with parameters

ruby-on-railsruby-on-rails-3

I have a restful (scaffold generated) controller and model for Votes, I am simply trying to add a field into the view that when clicked, will create a new vote for a given car. It needs to be a POST, What's the way I should be doing this in rails?

<% @cars.each do |car| %>
  <tr>
    <td><%= button_to '+', {:controller => "votes", :action => "create"}, :car_id => car.id, :user_id=> session[:user_id] , :method=>:post  %></td>
    <td><%= car.votes.count %></td>
    <td><%= car.name %></td>
    <td><%= car.code %></td>
    <td><%= car.album %></td>
<% end %>

Best Answer

<td><%= button_to '+', {:controller => "votes", :action => "create", :car_id => car.id, :user_id=> session[:user_id]} , :method=>:post  %></td>

This will make params[:car_id] and params[:user_id] available in VotesController create action.

Related Topic