Rails: Multi-submit buttons in one Form

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

提交回复
热议问题