Rails: Multi-submit buttons in one Form

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

提交回复
热议问题