Search code examples
ruby-on-railsrubyruby-on-rails-3ruby-on-rails-3.2ruby-on-rails-3.1

Rails: multiple submit button outside simple form


Say I have an Article model, and in the Article model 'settings' view I have two submit buttons outside of a form, "update Details" and "Next Template".

My question is how can I know which button is clicked in the controller. Both submit button is outside of a simple form. I tried like:

 <%= f.submit "update Details",name: "update_details", class: "x-update" %>


<%= f.submit 'Next Template', name: "next_template", class: "x-next" %>

and the logic is the same on the controller

   if params[:update_details]
      [..]
   elsif params[:next_template]
      [..]
   end

but it doesn't work. How do I do that? I can't change the route, so is there a way to send a different variable that gets picked up by [:params]?


Solution

  • I resolved it by putting in our html form template:

    <input type="hidden" name= "action_type" id="action_type">
    

    then in JavaScript file we added

    $('.x-update').on 'click', ->
    $("#action_type").val("exit")
    $("#details-form").submit()
    

    and then in controller file we checked the parameters:

    if params[:action_type] == "exit"
      redirect_to #your desired path
    else
      redirect_to #based on given path
    

    That's It. It works.