Rails link_to or button_to post request with parameters

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


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

Erez is right -- the first hash in this case is the "URL parameters" hash, which controls the URL where the button sends its request. The second hash is the "HTML parameters" hash, which controls the appearance of the button, as well as the submit method (by adding a hidden field in the generated HTML).

The confusing thing here is that the URL parameters ask you to specify the controller and action in a controller-centric way, but then requires you to pass along the id's for the URL, which is more URL-centric. That combination threw me off for a long time. You can add any additional parameters you like in the URL parameters hash, by the way -- to use as arguments for other methods in your controller, to take more advanced action.


Rails 5 + haml, for example:

= button_to "smth", some_path, method: :get, params: { start_point: 3.month.ago }

the key is to use params key, then in controller you will be able to get value via @some_var = params[:start_point]