Forgive me if this sounds like a commonly asked question, but I’ve
searched high and low and read all the books and I’m still struggling.
I’m trying to park the Data from an HTML form into a session and then
retrieve it. Maybe I’m trying to do the wrong, I dont know. The idea is
that they fill in the form, go to a confirmation screen, then go to a
final form where it’s submitted to the database. I’m not even sure if
thats the right logic. I think I need to park the data in a session. But
I cant see how.
All I can get in a session is one piece of data and I dont know how to
reference it.
Storing it on a cookie so when the user comes back they see it? Why not
store it as a record on the table and then get it the next time the user
logs in?
You could just do session[:thingo] = params and then reference
session[:thingo][:checkin]
Hmm… Ok, so I made my method like this:
def confirm_form
session[:formStore1] = params
end
With the view the same it now squirts out the entire cookie hash:
commitnextauthenticity_token35663ebb22fd497d1ab91470957fa140d8a09f24actionconfirm_formnights5checkin2controllerbooking_formcheckout4
How do I reference the value I need?
Storing it on a cookie so when the user comes back they see it? Why not
store it as a record on the table and then get it the next time the user
logs in?
Well… my business logic went something like this:
Screen 1. Get the user to select some dates
Screen 2. Get the user to confirm that those are the dates
Screen 3. Commit the dates and get their details
Which seemed sensible in my head. But may not be
I guess the idea being that in a “wizard” type process I would park the
data in a session before committing to the database.
On Jan 13, 2008, at 11:09 PM, Alex Counsell wrote:
This doesnt seem to do that:
<%= @checkin = session[:formStore1] %>
You are using the output ruby delimiter <%= . The best you can hope
for here is to get a true because the assignment of @checkin = session
[:checkin] succeeded. I guess I missed the code, but you will
probably want to do your
@checkin = session[:checkin]
in the controller action that is going to be rendering your view.
Then in your view, you will have the form tag/helper that renders the
form. At that point, you will use @checkin to get the data out,
whether it be a form_for or form_tag. I might be misunderstanding,
but it sounds like you are expecting Rails to automatically render
the contents of @checkin when you pull it out of session. It doesn’t
quite work like that.
I guess I’m trying to use the session-cookie as a general purpose
hash.
But maybe thats just wrong and, as you say, doesnt work like that.
I think you might have mistaken my meaning. I use session as a
general purpose hash all the time. I store everything in it, from
orders to other hashes to strings and integers (I can hear some
people hissing and booing now grin). What I meant by “it doesn’t
work that way” was that you can’t pull something out of session and
have Rails magically use it. For instance, if I store form fields
into session, I might do it something like this:
def create @my_obj = Obj.new(params)
<do some things with @my_obj if you need to>
session[:my_obj] = @my_obj
end
Now, when we get to the next step in the process, something like this
will happen
def step_two
get my_obj out of session so the view has access to it
@my_obj = session[:my_obj]
render whatever you need to
end
And let’s say the view is using a standard form_tag
You’d have to reference the specific attribute of @my_obj in your
form. Rails isn’t going to just know what to do with that object.
Now, if you’re using form_for, I might be mistaken as I haven’t used
that much (or at all).
At least, this is how I do it. Maybe I’m making it more difficult
than it needs to be…
At least, this is how I do it. Maybe I’m making it more difficult
than it needs to be…
Peace,
Phillip
Hmm, maybe
I dont know. I’m still utterly confused about how this works.
I think I need to create my array and store that array in the session.
Which makes sense. But for the life of me I cant figure out how to do
that. I can create Arrays and hashes and pull data out of them in Ruby,
but when it comes to Rails and using sessions it just all falls apart. I
cant make head-nor-tail of it.