Rails link_to or button_to post request with parameters

前端 未结 4 516
悲哀的现实
悲哀的现实 2020-12-24 06:52

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 c

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 07:36

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

    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.

提交回复
热议问题