# dissecting params to read data

**URL:** <https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800>\
**Category:** rubyonrails-talk\
**Created:** [March 25, 2008, 2:46am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800 "2008-03-25T02:46:16Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [March 25, 2008, 2:46am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/1 "2008-03-25T02:46:16Z")

</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"=\>"foo@bar.com"}, "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:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://discuss.rubyonrails.org/u/Steve_Ross)\
**Post date:** [March 25, 2008, 2:55am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/2 "2008-03-25T02:55:32Z")

</div>

@user['form2'][:important\_data]

Does this work?

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [March 25, 2008, 3:22am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/3 "2008-03-25T03:22:05Z")

</div>

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:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [March 25, 2008, 3:23am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/4 "2008-03-25T03:23:37Z")

</div>

Hi --

> 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"=\>"foo@bar.com"}, "form2"=\>{"important\_data"=\>"043"}, "commit"=\>"Register!", "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:

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

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

David

---

<div class="post-metadata">

**Author:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://discuss.rubyonrails.org/u/Steve_Ross)\
**Post date:** [March 25, 2008, 3:26am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/5 "2008-03-25T03:26:50Z")

</div>

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:** ![David\_A\_Black](https://avatars.discourse-cdn.com/v4/letter/d/dbc845/32.png) [@David\_A\_Black](https://discuss.rubyonrails.org/u/David_A_Black)\
**Post date:** [March 25, 2008, 3:28am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/6 "2008-03-25T03:28:49Z")

</div>

Hi --

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [March 25, 2008, 5:02am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/7 "2008-03-25T05:02:16Z")

</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 but i not sure how to implement with a drop down menu. thanks for all the suggestions

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [March 25, 2008, 5:04am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/8 "2008-03-25T05:04:32Z")

</div>

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:** ![Steve\_Ross](https://avatars.discourse-cdn.com/v4/letter/s/3d9bf3/32.png) [@Steve\_Ross](https://discuss.rubyonrails.org/u/Steve_Ross)\
**Post date:** [March 25, 2008, 5:59am UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/9 "2008-03-25T05:59:50Z")

</div>

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']

---

<div class="post-metadata">

**Author:** ![OhMyRuby](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OhMyRuby](https://discuss.rubyonrails.org/u/OhMyRuby)\
**Post date:** [March 25, 2008, 2:36pm UTC](https://discuss.rubyonrails.org/t/dissecting-params-to-read-data/26800/10 "2008-03-25T14:36:50Z")

</div>

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