Adding a Form Cancel Button

I'd like my create and edit forms to both have a Cancel button that
redirects back to the referring page. To date, the best approach I've
found is to include an additional submit_tag with a :name parameter of
cancel. Then inside the controller I have in if/else statement that
checks for the existence of params[:cancel]. If it exists, I issue a
redirect to the referring page. Otherwise, I proceed with the create/
update.

Is there a better way?

Why not just do javascript:history.back()?

PP Junty wrote:

eric, i didn't find a better solution either. i'd not choose the javascript solution for two reasons. because it depends on the client's configuration/behavior and, most importantly, because sometimes the user wants to see an explicit message from the server that the action was indeed canceled (if her credit card number was in the page, for example).

I use a filter -
   before_filter :check_for_cancel, :only => [:create, :update]

and a method

   def check_for_cancel
     if params[:commit] == 'Cancel'
       redirect_back_or_default.......
     end
   end

andrew, the problem i see is that you depend on the label of the button.
if a user wants to change the label of one button, let's say to "Abort",
then it requires another method for this particular button.
using

<%= submit_tag 'Any Label', :name => 'cancel' %>

and

def check_for_cancel
  unless params[:cancel].blank?
    redirect_back_or_default.......
  end
end

you can avoid that.

Andrew Porter wrote:

I just add a link next to the submit tag labelled 'cancel' with a link
to an appropriate page.
Technically a button should be used for an action while cancelling is
technically the absence of an action.

Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain

I totally agree. Using a link instead of a button has several
advantages. That the user does not expect anything destructive to
happen when clicking a link, is one. And you don't need to clutter
your controller with logic for the cancel button. The only thing you
need is a simple #link_to refering to an appropriate page.

At the risk of being contentious, I suggest that the above are non-Railsy answers. It may be strictly correct to say that Cancel is an instruction to do nothing and should not therefore be a button. Millions of people, however, are used to seeing cancel buttons all over the place and expect it to throw away any data they have entered on a form and ensure that they have done no harm. Rails stresses the importance of following conventions and should therefore follow this convention and not try to enforce unconventional ideas on the user.

In fact as I think about it, a Cancel button does take some action (at least from the users perspective, which is what matters), it discards any data he has entered into the form. The fact that this is a do-nothing action in the Rails code is irrelevant to what the user interface should be.

Colin

Well, I can see what you mean. I do not want to discuss it further,
just wanted to mention that you could use the #button_to method to
avoid cluttering your controller. Problem is, you need to put it
outside the form since the method outputs a <form> itself.