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
Share:
13,889
NullVoxPopuli
Author by

NullVoxPopuli

I do stuff

Updated on July 18, 2022

Comments

  • NullVoxPopuli
    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
    NullVoxPopuli about 14 years
    I did a puts params, and there isn't a :commit key in the params hash... =\
  • Tanel Suurhans
    Tanel Suurhans about 14 years
    What does the params hash contain?
  • NullVoxPopuli
    NullVoxPopuli about 14 years
    When I use :name there is no :commit. wehn i don't use :name, the :commit is the text of the button
  • Tomas Markauskas
    Tomas Markauskas about 14 years
    Then use the name instead of :commit.
  • Teemu Leisti
    Teemu Leisti over 12 years
    Works for me. Been looking all over for the answer to this trick. Thanks!
  • emery
    emery over 9 years
    Also see stackoverflow.com/questions/17916316/… if you want to pass a specific param to the controller depending on which button was clicked.