Rails: Multi-submit buttons in one Form

前端 未结 6 899
梦如初夏
梦如初夏 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: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
    

提交回复
热议问题