Adding a Form Cancel Button

879 views
Skip to first unread message

ericindc

unread,
Feb 21, 2009, 3:42:08 PM2/21/09
to Ruby on Rails: Talk
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?

Maurício Linhares

unread,
Feb 21, 2009, 10:48:29 PM2/21/09
to rubyonra...@googlegroups.com
Why not just do javascript:history.back()?

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)

PP Junty

unread,
Mar 16, 2009, 11:24:15 AM3/16/09
to rubyonra...@googlegroups.com
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).
--
Posted via http://www.ruby-forum.com/.

Andrew Porter

unread,
Mar 16, 2009, 12:26:25 PM3/16/09
to rubyonra...@googlegroups.com
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

PP Junty

unread,
Mar 16, 2009, 12:58:35 PM3/16/09
to rubyonra...@googlegroups.com
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 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

--
Posted via http://www.ruby-forum.com/.

Andrew Timberlake

unread,
Mar 17, 2009, 2:31:45 AM3/17/09
to rubyonra...@googlegroups.com
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

rub...@ymail.com

unread,
Mar 17, 2009, 3:13:38 AM3/17/09
to Ruby on Rails: Talk


On 17 Mar., 07:31, Andrew Timberlake <and...@andrewtimberlake.com>
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.

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.

--
Cheers,
David Knorr
http://twitter.com/rubyguy

Colin Law

unread,
Mar 17, 2009, 5:18:20 AM3/17/09
to rubyonra...@googlegroups.com

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

rub...@ymail.com

unread,
Mar 17, 2009, 10:52:48 AM3/17/09
to Ruby on Rails: Talk


On 17 Mar., 10:18, Colin Law <clan...@googlemail.com> wrote:
> 2009/3/17 ruby...@ymail.com <ruby...@ymail.com>
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.
Reply all
Reply to author
Forward
0 new messages