Ruby on Rails: How to have multiple submit buttons going to different methods (maybe with with_action?)
13,889
The submit button name attribute is passed to the controller as params[:commit]. So in your case:
if params[:commit] == "save"
end
![NullVoxPopuli](https://i.stack.imgur.com/INqgP.jpg?s=256&g=1)
Comments
-
NullVoxPopuli almost 2 years
So..
<%= submit_tag 'Save', :name => 'save' %> <%= submit_tag 'Save to Library', :name => 'library' %>
then in my controller:
with_action do |a| a.save do end a.library do end end
the problem is that only one of the actions is getting invoked... the same one for both submit_tags... any idea why?
or how I can get two buttons to submit a form to two different methods?
-
NullVoxPopuli about 14 yearsI did a puts params, and there isn't a :commit key in the params hash... =\
-
Tanel Suurhans about 14 yearsWhat does the params hash contain?
-
NullVoxPopuli about 14 yearsWhen I use :name there is no :commit. wehn i don't use :name, the :commit is the text of the button
-
Tomas Markauskas about 14 yearsThen use the name instead of
:commit
. -
Teemu Leisti over 12 yearsWorks for me. Been looking all over for the answer to this trick. Thanks!
-
emery over 9 yearsAlso see stackoverflow.com/questions/17916316/… if you want to pass a specific param to the controller depending on which button was clicked.