Forum: Ruby on Rails Adding a Form Cancel Button

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eric M. (Guest)
on 2009-02-21 22:43
(Received via mailing list)
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 L. (Guest)
on 2009-02-22 05:49
(Received via mailing list)
Why not just do javascript:history.back()?

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)
Py J. (Guest)
on 2009-03-16 17:24
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).
Andrew P. (Guest)
on 2009-03-16 18:27
(Received via mailing list)
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
Py J. (Guest)
on 2009-03-16 18:58
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 P. 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
Andrew T. (Guest)
on 2009-03-17 08:32
(Received via mailing list)
On Mon, Mar 16, 2009 at 6:58 PM, PP Junty
<removed_email_address@domain.invalid> wrote:
> def check_for_cancel
>> I use a filter -
> --
> Posted via http://www.ruby-forum.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 T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
(Guest)
on 2009-03-17 09:14
(Received via mailing list)
On 17 Mar., 07:31, Andrew T. <removed_email_address@domain.invalid>
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 K.
http://twitter.com/rubyguy
Colin L. (Guest)
on 2009-03-17 11:19
(Received via mailing list)
2009/3/17 removed_email_address@domain.invalid 
<removed_email_address@domain.invalid>

> 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
(Guest)
on 2009-03-17 16:53
(Received via mailing list)
On 17 Mar., 10:18, Colin L. <removed_email_address@domain.invalid> wrote:
> > > I just add a link next to the submit tag labelled 'cancel' with a link
> At the risk of being contentious, I suggest that the above are non-Railsy
> 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.

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.

--
Cheers,
David K.
http://twitter.com/rubyguy
This topic is locked and can not be replied to.