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
Instead of a submit tag, I would suggest a tag, because the parameter
:commit
might be localised, thus making it a pain to evaluate in the controller.
A button_tag
can have a name/value like any other field, so you can basically create a flag with it.
The following code is what I would use:
in the view:
<%= button_tag t('buttons.cancel'), type: "submit", name: "cancel", value: true %>
in the controller:
before_filter :redirect_cancel, only: [:create, :update]
private
def redirect_cancel
redirect_to my_page_path if params[:cancel]
end
Much cleaner imho, because you rely on a flag instead of a (potentially) localised display value.