Ruby on Rails : how to implement Cancel button in form_tag

后端 未结 4 1278
情书的邮戳
情书的邮戳 2021-02-07 15:28

I have a basic form using the form_tag helper working fine, but I want to add a cancel button, what is the syntax for doing this? I want the cancel button to appear as a button

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-07 16:01

    If you want to clear/reset the form fields, do what dchacke suggests.

    However, I'd generally expect a Cancel button to not clear the form, but to take me away from the form, meaning I don't plan on submitting it.

    If you want the latter, I'd just make a link (or button) to the page you want to go to upon cancelling, such as :

    =link_to 'Cancel', my_page_path
    

    Or if you want a button:

    = button_tag "Cancel", :type => 'button'
    

    Then add this to your controller:

    before_filter :check_for_cancel, :only => [:create, :update]
    
    def check_for_cancel
      if params[:commit] == "Cancel"
        redirect_to my_page_path
      end
    end
    

提交回复
热议问题