Ye Olde Form, Sessions and Params hash type questions

Hi,

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.

Here’s what I have:

This is in my Controller

def confirm_form
  @checkin = params[:checkin]
  @checkout = params[:checkout]
  @nights = params[:nights]
end

and this is small snippet of my form:

        <% form_tag(:action => "confirm_form") do %>
        <p>checkin:<%= text_field_tag("checkin") %></p>
        and so on...
        <%= submit_tag("next") %>

which works and can be retrieved on the next page like this:

     Checkin: <%= @checkin %> <br>
     Checkout <%= @checkout %> <br>
     Nights: <%= @nights %> <br>

But how do I put that data in a session to be retrieved when ever I
want?

I’m struggling here, this is confusing the heck out of me.

Maybe I should be just parking it all in a DB. i just dont know.

Thank for any help at all.

Alex.

session[:checkin] = @checkin

On Jan 14, 2008 11:00 AM, Alex Counsell
[email protected]
wrote:

thats the right logic. I think I need to park the data in a session. But
@checkin = params[:checkin]

Maybe I should be just parking it all in a DB. i just dont know.

Thank for any help at all.

Alex.

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Ryan B. wrote:

session[:checkin] = @checkin

This is whats confusing me. I cant figure out the logic to put all the
form fields into the session, and then retrieve that session.

If I make the method:

def confirm_form
session[:checkin] = @checkin
end

How do I then get that data back out?

I’ll do some more searching…

Thanks, Alex

On Jan 13, 2008, at 7:43 PM, Alex Counsell wrote:

def confirm_form
session[:checkin] = @checkin
end

How do I then get that data back out?

Once you do

session[:checkin] = @checkin

then session[:checkin] is a reference to the same object that
@checkin was. So if you have a name attribute on @checkin

@checkin.name

you can do

session[:checkin].name

or better

@checkin = session[:checkin]

to get all of it out at once.

Peace,
Phillip

<% form_tag(:action => "confirm_form3") do %>

Ignore that typo where I put _form3. :slight_smile:

It’s your

def confirm_form
session[:checkin] = @checkin
end

That’s buggered.

You should be referencing it as params[:checkin], not @checkin.

On Jan 14, 2008 1:53 PM, Alex Counsell
[email protected]
wrote:

<% form_tag(:action => "confirm_form3") do %>

Ignore that typo where I put _form3. :slight_smile:

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Whoops, that’s what I suggested :slight_smile: What I meant was you were definining
@checkin as params[:checkin] before, so now it should be:

session[:checkin] = params[:checkin]

On Jan 14, 2008 2:03 PM, Ryan B. [email protected] wrote:


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

@checkin = session[:checkin]

to get all of it out at once.

Peace,
Phillip

Cool, OK - somethings not right then…

So the action for my form_tag is:

<% form_tag(:action => "confirm_form3") do %>

Which calls this method in my controller:

def confirm_form
session[:checkin] = @checkin
end

Then I have a view called “confirm_form.html.erb” onto which I put:

<%= @checkin = session[:checkin] %>

And that should render the values?

If I do: <%= session[:checkin].checkout %>

I get the “The error occurred while evaluating nil.checkout” error.

Which makes me think that it doesnt know about the ‘checkin’ session?

In my form I put:

      <% form_tag(:action => "confirm_form") do %>
      <p>checkin:<%= text_field_tag("checkout") %></p>
      <%= submit_tag("next") %>

I think I must be missing something vital here.

Thanks for any help.

Alex.

Ryan B. wrote:

Whoops, that’s what I suggested :slight_smile: What I meant was you were definining
@checkin as params[:checkin] before, so now it should be:

session[:checkin] = params[:checkin]

On Jan 14, 2008 2:03 PM, Ryan B. [email protected] wrote:

Ok, excellent. I think I’m getting somewhere:

So I need to set a session per form-field?

Like this:

def confirm_form
session[:formStore1] = params[:checkin]
session[:formStore2] = params[:checkout]
session[:formStore3] = params[:nights]
end

which I then display like this:

Checkin: <%= @checkin = session[:formStore1] %>

Checkout <%= @checkout = session[:formStore2] %>

Nights: <%= @nights = session[:formStore3]%>

That seems long winded to me…

But works.

Thanks, Alex

You could just do session[:thingo] = params and then reference
session[:thingo][:checkin]

On Jan 14, 2008 3:00 PM, Alex Counsell
[email protected]
wrote:

which I then display like this:

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

I guess a bonus question is:

How do you store an Array or Hash in a Cookie?

Or is that a trick question and the answer is you shouldnt?

I think I just assumed 'Rails would put all my form-field values into
the Cookie for me and allow me to extract them as needed.

I think maybe my fundamental understanding of how the session coookie
store works might be lacking :slight_smile:

It’s hard to find solid references for this stuff…

Thanks for the help.

Alex

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?

On Jan 14, 2008 3:17 PM, Alex Counsell
[email protected]
wrote:

I think maybe my fundamental understanding of how the session coookie


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Ryan B. wrote:

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?

This doesnt seem to do that:

<%= @checkin = session[:formStore1] %>

Which will just get the entire cookie.

Cheers, Alex

Ryan B. wrote:

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 :slight_smile:
I guess the idea being that in a “wizard” type process I would park the
data in a session before committing to the database.

Cheers, Alex

what does <%= debug session.inspect %> give?

On Jan 14, 2008 3:39 PM, Alex Counsell
[email protected]
wrote:

Which will just get the entire cookie.

Cheers, Alex

Posted via http://www.ruby-forum.com/.


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Ryan B. wrote:

what does <%= debug session.inspect %> give?

An enormous string that goes on and one. here’s a snippet:

Session:0x18dbcb4 @data={:formStore=>“2”,
:formStore1=>{“commit”=>“next”,
“authenticity_token”=>“35663ebb22fd497d1ab91470957fa140d8a09f24”,
“action”=>“confirm_form”, “nights”=>“5”, “checkin”=>“2”,
“controller”=>“booking_form”, “checkout”=>“4”}, :checkin=>“8”,
:formStore2=>“b”, :csrf_id=>“67653f2742e8057138188392d7777064”,
:formStore3=>“c”, “flash”=>{}},
@dbprot=[#<CGI::Session::CookieStore:0x18db764 @data={:formStore=>“2”,
:formStore1=>{“commit”=>“next”,
“authenticity_token”=>“35663ebb22fd497d1ab91470957fa140d8a09f24”,
“action”=>“confirm_form”, "nights…

Al

the contents of @checkin when you pull it out of session. It doesn’t
quite work like that.

Hmm… sort of. I’m not sure.

It seems that if I want to store form fields in a session-cookie then
I’m only managing to do it like this:

def confirm_form
session[:formStore1] = params[:checkin]
session[:formStore2] = params[:checkout]
session[:formStore3] = params[:nights]
end

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.

Do you know of any references that can explain how it does work?

Cheers, Alex

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.

Hope that helps.

Phillip

On Jan 14, 2008, at 4:19 PM, Alex Counsell wrote:

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

form_tag :action => :step_two_save
text_field_tag :some_value, @my_obj.some_value
text_field_tag :some_other_value, @my_obj.some_other_value
end

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…

Peace,
Phillip

Hey

def create
@my_obj = Obj.new(params)

…snip

At least, this is how I do it. Maybe I’m making it more difficult
than it needs to be…

Peace,
Phillip

Hmm, maybe :slight_smile:

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.

Oh well. :frowning:

Thanks, alex