Ye Olde Form, Sessions and Params hash type questions

On Jan 16, 2008, at 12:12 AM, Alex Counsell wrote:

cant make head-nor-tail of it.

Hi Alex,

You should be able to do this

session[:my_array] = Array.new

session[:my_array] << an_element

copy_of_element = session[:my_array][0]

Does that not work?

Peace,
Phillip

H Phillip,

I’m probably doing something wrong somewhere…

The only way I’ve managed to get the values in and out is like this:

Here’s my original ‘index.htm.erb’ form code

<% form_tag(:action => “confirm_form”) do %>

checkin:<%= text_field_tag("checkin") %>

checkout:<%= text_field_tag("checkout") %>

nights:<%= text_field_tag("nights") %>

<%= submit_tag("next") %> <% end %>

And here’s the controller code from ‘booking_form_controller.rb’

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

and then my ‘confirm_form.html.erb’ view code looks like this:

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

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

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

Which did all seem rather verbose.

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 :slight_smile:

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. :confused:

Thanks, Alex

Phillip K. wrote:

On Jan 16, 2008, at 12:12 AM, Alex Counsell wrote:

cant make head-nor-tail of it.

Hi Alex,

You should be able to do this

session[:my_array] = Array.new

session[:my_array] << an_element

copy_of_element = session[:my_array][0]

Does that not work?

Peace,
Phillip

Phillip You are a genius :slight_smile:

That worked a treat. Thanks.

I’ll have look up “HashWithIndifferentAccess” and “merge!” to see what
they do.

Thanks for the help. Much appreciated.

Cheers, Alex

Phillip K. wrote:

On Jan 16, 2008, at 5:57 PM, Alex Counsell wrote:

And here’s the controller code from ‘booking_form_controller.rb’

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

try this

def confirm_form
session[:formStore] = HashWithIndifferentAccess.new if session
[:formStore].nil?

session[:formStore].merge!(params)
end

and then my ‘confirm_form.html.erb’ view code looks like this:

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

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

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

and this

Checkin: <%= session[:formStore][:checkin] %>

Checkout: <%= session[:formStore][:checkout] %>

Nights: <%= session[:formStore][:nights] %>

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.

Peace,
Phillip

On Jan 16, 2008, at 5:57 PM, Alex Counsell wrote:

And here’s the controller code from ‘booking_form_controller.rb’

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

try this

def confirm_form
session[:formStore] = HashWithIndifferentAccess.new if session
[:formStore].nil?

session[:formStore].merge!(params)
end

and then my ‘confirm_form.html.erb’ view code looks like this:

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

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

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

and this

Checkin: <%= session[:formStore][:checkin] %>

Checkout: <%= session[:formStore][:checkout] %>

Nights: <%= session[:formStore][:nights] %>

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.

Peace,
Phillip

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

Phillip You are a genius :slight_smile:

Um, actually, I just had a memory of having to do the same thing two
weeks ago :slight_smile:

That worked a treat. Thanks.

Wonderful!

I’ll have look up “HashWithIndifferentAccess” and “merge!” to see what
they do.

I’ll save you a little bit of digging. HashWithIndifferentAccess is
a class that Rails provides which allows you to access a value by
using either a string key or symbol key. So params[:id] is the same
as params[‘id’] because params is a HashWithIndifferentAccess.
That’s the bear that I wrestled with a couple of weeks ago (thanks
again ///ark!), but once I learned about HWIA, all is much better.

As for merge!, take a look at

http://www.ruby-doc.org/core/

and scroll down the Class list (in the middle) to Hash.

Peace,
Phillip