Rails: Multi-submit buttons in one Form

前端 未结 6 898
梦如初夏
梦如初夏 2020-11-29 19:07

Say I have an Article model, and in the article \'new\' view I have two buttons, \"Publish\" and \"Save Draft\".

My question is how can I know which button is clicke

相关标签:
6条回答
  • 2020-11-29 19:21

    it can also be done on the form_for helper like this

     <%= f.submit "Publish",name: "publish", class: "tiny button radius success" %>
     <%= f.submit 'Mark as Draft', name: "draft", class: "tiny button radius " %>
    

    and the logic is the same on the controller

       if params[:publish]
          // your code
       elsif params[:draft]
          // your code
       end
    
    0 讨论(0)
  • 2020-11-29 19:24

    We solved using advanced constraints in rails.

    The idea is to have the same path (and hence the same named route & action) but with constraints routing to different actions.

    resources :plan do
      post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
      post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize
    end
    

    CommitParamRouting is a simple class that has a method matches? which returns true if the commit param matches the given instance attr. value.

    This available as a gem commit_param_matching.

    0 讨论(0)
  • 2020-11-29 19:25

    You could also set some data attributes on the submit buttons and use JavaScript to change out the form action on click of one of the buttons

    0 讨论(0)
  • 2020-11-29 19:28

    I remember coming across this problem once. You cannot keep two buttons and then call some action based on the params[:commit]. the submit button onclick is going to call the url the form refers to. There are certain bad ways to get the desired behavior. Keep a button to call the action the form refers to and to get another button to call a action, I used a link_to and then changed the styles to match a button. Also, alternatively you can use jQuery to change the url the form would call, hence deciding what action is invoked at run-time. Hope this helps.

    0 讨论(0)
  • 2020-11-29 19:32

    This was covered in Railscast episode 38. Using the params hash to detect which button was clicked is the correct approach:

    View:

    <%= submit_tag 'Create' %>
    <%= submit_tag 'Create and Add Another', name: 'create_and_add' %>
    

    Controller:

    if params[:create_and_add]
      # Redirect to new form, for example.
    
    else
      # Redirect to show the newly created record, for example.
    end
    
    0 讨论(0)
  • 2020-11-29 19:42

    usually i using the suggestion given by John Topley (see answer above). another way is using JQuery /JS changing the form action attribute- upon clicking the submit button example:

    form_tag({} ,:method => 'post', :id => 'reports_action') do 
       .......
       ....... 
      submit_tag 'submit',  :onclick => "return changeAction();"
    end
    

    and then .....

    function changeAction(){
       $('#reports_action').attr('action','my_new_action');
    }   
    
    0 讨论(0)
提交回复
热议问题