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?
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 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.
I'd assumed I could pass the key value pair into a hash stored in the
Session, then reference the key to pull out the value. I think I'm
probably just struggling with the syntax a little - maybe I'm not
understanding how the the form results are being passed into my
confirm_form method. I'm not sure really
But what I have works, just doesnt seem right or very nice to me. I
couldnt get an joy trying to pass the key and values from the from into
an array as you described. It kept spitting out the key and value.
But now that I see what you're trying to do, you don't really need to shove that into session. If all you want to do is redisplay what the user entered, you can access params over in the confirm_form.html.erb view without bothering with session. Unless you have redirected or something. If you redirect, you will need to use session.