This is possibly a newbie question, but I’m not sure what terms to search for.
Say I have a CUSTOMER object, and I want to send a MESSAGE to that customer.
What I would do first is add a SENDMESSAGE action on the CUSTOMER controller, which builds the message object. (Assume this is the right thing to do?)
In this instance however, rather than actually send the message from within this action, I need to forward to the edit view of the MESSAGE to capture the body text etc.
The question: I want to do this without persisting the object. I want to build the object here and then hand it over to another view for completion.
def sendmessage
@message = Message.new
@message.title = 'WIBBLE'
@message.thecustomer = self
@message.save
respond_to do |format|
format.html { redirect_to(edit_message_path(@ message)) }
format.xml { render :xml => @ message }
end
end
Maybe my question boils down to, what is the ‘rails way’ to cache parameters and objects across requests and multiple screens.
Happy to be pointed towards Web URLs as I expect this is simple.
Thanks
The standard way to persist data across requests when building Web applications is to use the HTTP session. Rails makes an implicit session hash available for this purpose. It’s used like this:
You can also use the Rails flash session wrapper for passing information from the current action to the next. It’s usually used for storing text to be displayed in the UI, but you can use it for persisting any object:
In both cases, the objects that you’re storing must be serializable. Note that by default Rails stores session data in an encrypted cookie on the client; consider it a strong hint that storing a lot of data in the session is frowned upon in the Rails world.