SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Rails: Add form button, cancel action?

    I've looked for this on google, but didn't find anything. How can I add a cancel action to a form? When I add a submit_tag, it just does an update or new action.

    - matt

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can look around here:
    http://api.rubyonrails.com/

    My 5 minute search looking in the FromHelpers and other various views turned up nothing. You could always write your own helper though, a clear button is not too hard

  3. #3
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I normally implement cancel buttons as a javascript button (i know it's sorta evil) in most cases as they are on admin backends. Otherwise, I guess you can make the controller decide what to do based on the value of the submit tag

  4. #4
    SitePoint Enthusiast Stevenwulf's Avatar
    Join Date
    May 2002
    Location
    Berkeley
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a cancel button & a submit button

    <%= submit_tag 'Cancel' %>
    <%= submit_tag 'Update' %>


    Your controller can differentiate which button was pressed too:

    def edit()

    if request.get?
    @institution = Institution.find(params[:id])
    elsif request.post?
    if params[:commit].eql?('Cancel')
    redirect_toaction=>'view', :id=>params[:id])
    return nil
    else
    @institution = Institution.find(params[:id])
    if @institution.update_attributes(params[:institution])
    flash[:notice] = 'Institution was successfully updated.'
    redirect_toaction=>'view', :id=>@institution)
    end
    end
    end
    end

  5. #5
    SitePoint Addict Brak's Avatar
    Join Date
    Jul 2004
    Location
    Central Coast, CA
    Posts
    321
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cancel buttons are step-dependent and application-dependent.

    Suppose, for example you have a shopping cart. At the first step (say, log in) cancel should probably send you back to the cart screen (the last page). But say 3 steps in, should it still redirect to the previous screen? No, it should go to the cart screen again.

    Thus, no helper tags Use something similar to Sevenwulf suggested and you should be good to go.
    Studio Rockstar's Blog - A journey to quitting the dayjob.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •