Merging params into another hash

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

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

Five more minutes. I couldn't have thought about it for five more minutes!

params is a HashWithIndifferentAccess while my session[:people_search] was just a plain old Hash. When I changed the instantiation of session[:people_search] to HashWithIndifferentAccess, it works.

*sigh*

Just for my education, would changing the keys from symbols to strings also have worked?

///ark

Well, apparently for my education, too. Changing the keys from symbols to strings does, in fact, work.

Why?

Phillip

params is a HashWithIndifferentAccess while my session [:people_search] was just a plain old Hash. When I changed the instantiation of session[:people_search] to HashWithIndifferentAccess, it works.

Just for my education, would changing the keys from symbols to
strings also have worked?

Well, apparently for my education, too. Changing the keys from symbols to strings does, in fact, work.

Because in the HashWithIndifferentAccess you get as params the keys
are, deep down on the inside, strings, so when you merged that into
your array you got a bunch of string keys but you were looking for
symbol keys.

Fred

What Fred said. :slight_smile:

When I saw a class called HashWithIndifferentAccess, I just had to look into it. What a name! It's like "I couldn't care less whether you access me or not, but if you do, it really doesn't matter if it's with symbols or strings. It's all the same to me. Yawn."

///ark