Merging params into another hash

Phillip Koebbe
Fri, 28 Dec 2007 19:56:04 -0000

I have a search form that submits three parameters via an xhr
request.  The three parameters are full_name, email, and postal_code.
In the controller action, I want to stuff those values into the
session so they can be accessed later to repopulate the search form.

I start by making sure there is a place in session

session[:people_search] = Hash.new if session[:people_search].nil?

When I do this

session[:people_search][:full_name] = params[:full_name]
session[:people_search][:email] = params[:email]
session[:people_search][:postal_code] = params[:postal_code]

Everything works as I want it to.  But when i do this

session[:people_search].merge!(params)

or

session[:people_search] = session[:people_search].merge(params)

or

session[:people_search] = params

it does not work.

In case you''re curious, I don''t mind the other keys that exist
in
params.  I''m just trying to find the easiest and most portable way to
get things out of params into session.

Can someone explain to me why the direct assignment of each
individual key works but none of the other methods do.

Thanks,
Phillip