# Dissecting params to read data

**URL:** <https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826>\
**Category:** Rails\
**Created:** [March 25, 2008, 3:46am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826 "2008-03-25T03:46:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 3:46am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/1 "2008-03-25T03:46:57Z")

</div>

i have a simple form with a drop down menu that is created via another  
model. when the user hits submit i have all the data passed thru but  
can not extract it properly

form\_for :user do |form|  
form fields here…  
find from another table with another model that creates a drop down  
with form name “form2”  
end

data shows

{“user”=\>{“user\_zid”=\>“8780743182”,  
“minitial”=\>"",  
“lname”=\>“doe”,  
“fname”=\>“joe”,  
“password”=\>“xxxxx”,  
“email”=\>“removed\_email\_address@domain.invalid”},  
“form2”=\>{“important\_data”=\>“043”},  
“commit”=\>“Register!”,  
“authenticity\_token”=\>“d494c16b36637012c4d9d333a6045f3d4a0394cc”}

i tried:

@user = params[:user]  
and then  
need\_data = @user.form2[:important\_data]

help!

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 3:56am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/2 "2008-03-25T03:56:20Z")

</div>

On Mar 24, 2008, at 7:46 PM, rashantha wrote:

> @user = params[:user]  
> and then  
> need\_data = @user.form2[:important\_data]

@user[‘form2’][:important\_data]

Does this work?

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 4:22am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/3 "2008-03-25T04:22:41Z")

</div>

On Mar 24, 7:55 pm, “s.ross” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Mar 24, 2008, at 7:46 PM, rashantha wrote:

> @user[‘form2’][:important\_data]
> 
> Does this work?

no. You have a nil object when you didn’t expect it!

i have tried @user.form2[:important\_data]  
undefined form2 …

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 4:27am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/4 "2008-03-25T04:27:22Z")

</div>

On Mar 24, 2008, at 8:22 PM, rashantha wrote:

> i have tried @user.form2[:important\_data]  
> undefined form2 …

From your dump of params, you should be able to get your info using:

params[‘user’][‘form2’][‘important\_data’]

Nested params take some care because symbols can only be used to  
select the first level of the hash (IIRC). String keys always work,  
but the kewl kidz don’t use them 😉

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 4:29am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/5 "2008-03-25T04:29:43Z")

</div>

Hi –

On Mon, 24 Mar 2008, David A. Black wrote:

> As S. Ross said, you would need @user[:form2][:important\_data]. But

I mean [:user], not @user, of course – but really I mean the other  
suggestion I made 🙂

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 6:03am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/6 "2008-03-25T06:03:02Z")

</div>

> \<% form\_for :user do |form| %\>  
> \<%= form.text\_field … %\>  
> \<% fields\_for :user2 do |form2| %\>  
> \<%= form2.text\_field … %\>  
> \<% end %\>  
> \<% end %\>
> 
> and then you’ll have params[:user] and params[:user2] separately.

i tried all the solution but i am getting - You have a nil object when  
you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.

i did read about fields\_for and also saw it implemented on  
[railscast.com](http://railscast.com) but i not sure how to implement with a drop down menu.  
thanks for all the suggestions

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 6:05am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/7 "2008-03-25T06:05:24Z")

</div>

On Mar 24, 8:26 pm, “s.ross” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> From your dump of params, you should be able to get your info using:
> 
> params[‘user’][‘form2’][‘important\_data’]

wish it did but just wouldn’t

just to make sure i did

@finally = params[‘user’][‘form2’][‘important\_data’]

but get nil, request parameters show as per my first post.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 4:24am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/8 "2008-03-25T04:24:27Z")

</div>

Hi –

On Mon, 24 Mar 2008, rashantha wrote:

> “authenticity\_token”=\>“d494c16b36637012c4d9d333a6045f3d4a0394cc”}
> 
> i tried:
> 
> @user = params[:user]  
> and then  
> need\_data = @user.form2[:important\_data]

As S. Ross said, you would need @user[:form2][:important\_data]. But  
also remember that you can do:

\<% form\_for :user do |form| %\>  
\<%= form.text\_field … %\>  
\<% fields\_for :user2 do |form2| %\>  
\<%= form2.text\_field … %\>  
\<% end %\>  
\<% end %\>

and then you’ll have params[:user] and params[:user2] separately.

David

–  
Upcoming Rails training from David A. Black and Ruby Power and Light:  
ADVANCING WITH RAILS April 14-17 New York City  
INTRO TO RAILS June 9-12 Berlin  
ADVANCING WITH RAILS June 16-19 Berlin  
CORE RAILS June 24-27 London (Skills Matter)  
See [http://www.rubypal.com](http://www.rubypal.com) for more info!

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/9 "2008-03-25T15:37:41Z")

</div>

On Mar 24, 10:59 pm, “s.ross” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My mistake. I read the form2 as part of the user form, but now see  
> that it is a completely different one. Or at least that’s what I  
> deduce from the naming. In any case, the immediate solution to your  
> problem is to use:
> 
> params[‘form2’][‘important\_data’]

YEEEEEESSSSSSSSS!  
it worked thanks so much for the help ross and david. this was my  
first post on the group. thanks again.

---

<div class="post-metadata">

**Author:** ![rashantha](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b9bd4f/32.png) [@rashantha](https://www.ruby-forum.com/u/rashantha)\
**Post date:** [March 25, 2008, 7:00am UTC](https://www.ruby-forum.com/t/dissecting-params-to-read-data/132826/10 "2008-03-25T07:00:32Z")

</div>

On Mar 24, 2008, at 10:04 PM, rashantha wrote:

> @finally = params[‘user’][‘form2’][‘important\_data’]
> 
> but get nil, request parameters show as per my first post.

My mistake. I read the form2 as part of the user form, but now see  
that it is a completely different one. Or at least that’s what I  
deduce from the naming. In any case, the immediate solution to your  
problem is to use:

params[‘form2’][‘important\_data’]
