Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails link_to or button_to post request with parameters

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 %> 
like image 261
Cadyfatcat Avatar asked Sep 10 '12 22:09

Cadyfatcat


1 Answers

<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.

like image 183
Erez Rabih Avatar answered Sep 19 '22 23:09

Erez Rabih