SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing variables to a form from the controller

    In the controller I have an edit and delete (destroy) method. When I have carried out the action I want to pass a set of variables to a basic "success" view that describes what the success was (ie. Success - you have "deleted a record" or Success - you have "saved the edited record") with the things in quotes being the strings (plus others) that I want to pass. Here is the basic "destroy" method
    Code:
    def destroy
        EdaOfficer.find(params[:id]).destroy
        #(variavles to be passed to success form)
        redirect_to :action => 'success'
      end
    Can someone give me a hint how to do this.
    Ruby, Ruby when will you be mine

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Usually for messages such as those, you should generate the message in the destroy method and pass it on using flash variables.

    http://api.rubyonrails.com/classes/A...ler/Flash.html
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Member Arto's Avatar
    Join Date
    Mar 2005
    Location
    Spain
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mitch's comment describes the normal way to do this, but assuming you have other requirements, it's still pretty simple:

    Code:
    def destroy
      EdaOfficer.find(params[:id]).destroy
      redirect_to :action => 'success', :message => 'the record was deleted'
    end
    ...so, just pass parameters as normal, and in the 'success' action, grab them from the params hashtable, e.g. params[:message].

  4. #4
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only problem with that is that the message will be passed in the query string which is a bit smelly - flash is really the best way of doing this.

  5. #5
    SitePoint Zealot ricklach's Avatar
    Join Date
    Nov 2004
    Location
    Victoria BC
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. It worked just as I wanted and filled the bill. I did encounter one piece of unexpected behaviour. The "success.rhtml" file is a popup and lets the user know the action has been successful. It has a close popup button with the following code
    Code:
    <%= button_to_function 'Close Popup', onclick="window.opener.location.reload(true); window.close()" %>
    . When I create a new record and click "close popup" it closes the popup and refreshes the parent window. When I delete a record it only closes the popup - it does not refresh the parent. Same code different behaviour - curious???
    Ruby, Ruby when will you be mine


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
  •