# Help With Dependent Drop-Down and @Params

**URL:** <https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311>\
**Category:** Rails\
**Created:** [April 9, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311 "2006-04-09T16:33:24Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/1 "2006-04-09T16:33:24Z")

</div>

Hello All:

I am trying to create a select box that is filled with options based on  
the user’s selection in a related drop-down. I have had success with  
this before using the examples provided by the following two sources:

One posted by Rory at Rory on Rails - [http://www.roryhansen.ca/?p=9](http://www.roryhansen.ca/?p=9)

the other posted by Adam at  
[http://www.adamhegedus.com/articles/2006/01/19/ajaxed-select-boxes-in-rails](http://www.adamhegedus.com/articles/2006/01/19/ajaxed-select-boxes-in-rails)

Here is my problem:  
The user selects a Utility and a select with Receiving Points is updated  
with the items related to the specific utility. Here is my code…

\<\>  
def fill\_rec\_points  
@lookups = Lookup.find\_all\_by\_list\_prompt(@params[:utility], :order  
=\> ‘list\_item’)  
render :partial =\> ‘rec\_points’  
#I use a partial here to fill the span so as not to mix View and  
Business logic in the controller…  
end

\<\>

Utility  
  
\<%= select ‘deal’, ‘utility’, %w{ PG&E SoCal SDG&E Southwest},  
:include\_blank =\> true %\>

```
<span id="rec_point_list">
  <label for="deal_rec_point">Receipt Point</label><br/>
  <select id="deal[rec_point]" name="deal[rec_point]"></select>
</span>

```

\<%= observe\_field(“deal[utility]”,  
:frequency =\> 0.25,  
:update =\> “rec\_point\_list”,  
:url =\> {:action =\> :fill\_rec\_points},  
:with =\> “‘list\_item=’+value”) %\>

The problem seems to be that the controller is not picking anything up  
from the @params[:utility] reference. I have tried every variation of  
this I know (i.e. @params[‘deal’]['utility], @params[“utility”], etc.);  
all to no avail.

I know everything is set up right with the prototype.js stuff because  
when I manually enter the utility string in the controller (i.e.  
‘SoCal’) instead of @params[:utility] (or whatever else I have tried),  
it works great. I just can’t seem to get the user’s selection to pass to  
my method. Any ideas?

One last thought…when I have gotten this to work before in other  
projects I have passed an id integer and not a string like I am trying  
to do now, so maybe I am referencing my @params wrong? Thanks to All and  
sorry for a long post…

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/2 "2006-04-09T17:00:47Z")

</div>

Hi David,

My first shot would be to try it with ‘params’ instead of ‘@params’.  
The  
‘@’ is being deprecated.

Let us know if that helps.

Bill  
----- Original Message -----  
From: “David H.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: 2006-04-09 9:33 AM  
Subject: [Rails] Help With Dependent Drop-Down and @Params

> Hello All:
> 
> I am trying to create a select box that is filled with options based on  
> the user’s selection in a related drop-down. I have had success with  
> this before using the examples provided by the following two sources:
> 
> One posted by Rory at Rory on Rails - [http://www.roryhansen.ca/?p=9](http://www.roryhansen.ca/?p=9)
> 
> the other posted by Adam at

[http://www.adamhegedus.com/articles/2006/01/19/ajaxed-select-boxes-in-rails](http://www.adamhegedus.com/articles/2006/01/19/ajaxed-select-boxes-in-rails)

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/3 "2006-04-09T17:15:33Z")

</div>

Bill W. wrote:

> Hi David,
> 
> My first shot would be to try it with ‘params’ instead of ‘@params’.  
> The  
> ‘@’ is being deprecated.
> 
> Let us know if that helps.
> 
> Bill
> 
> >

Thanks Bill…I have tried that variation as well…same result. For  
some reason, I have trouble referencing form fields with the following  
name or id setting: model[object], which the \<% select %\> outputs in the  
html (in my case it outputs deal[utility]. I have read documentation on  
using params and @params which refers to the field via @params[:utility]  
or @params[“utiliy”].

I have had this working passing an id integer but am stuck trying it  
with a string…any other ideas? Thanks…

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 5:52pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/4 "2006-04-09T17:52:24Z")

</div>

@params[“deal”][“utility”] maybe? I do the same thing with a select but  
don’t have my code handy right now.

David H. wrote:

> Thanks Bill…I have tried that variation as well…same result. For  
> some reason, I have trouble referencing form fields with the following  
> name or id setting: model[object], which the \<% select %\> outputs in the  
> html (in my case it outputs deal[utility]. I have read documentation on  
> using params and @params which refers to the field via @params[:utility]  
> or @params[“utiliy”].
> 
> I have had this working passing an id integer but am stuck trying it  
> with a string…any other ideas? Thanks…

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/5 "2006-04-09T17:56:37Z")

</div>

> The B. wrote:
> 
> > @params[“deal”][“utility”] maybe? I do the same thing with a select but  
> > don’t have my code handy right now.
> 
> Barge…tried that to…nada. Thanks for the suggestion. It is weird.
> 
> If you could get a copy of your code to post? Maybe looking at the whole  
> thing would help…

Also…when I try @params[“deal”][“utility”] I get an error for an  
undefined method. Can’t evaluate nil.[]…???

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/6 "2006-04-09T17:54:53Z")

</div>

The B. wrote:

> @params[“deal”][“utility”] maybe? I do the same thing with a select but  
> don’t have my code handy right now.

Barge…tried that to…nada. Thanks for the suggestion. It is weird.

If you could get a copy of your code to post? Maybe looking at the whole  
thing would help…

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/7 "2006-04-09T18:26:58Z")

</div>

OK. This is a case where you need to have a view for this controller,  
and in the view you place:

```
<%= params.debug %>
```

That will show you the makeup of the params object, so that you can  
figure out how you need to reference the object in the hash.

If the HTML being produced shows a form field with name=“deal  
[utility]”, that tells Rails to create a :deal hash, within the  
params hash, with one entry using :utility as it’s key. Referencing  
params[:deal][:utility] should return the value of that entry…

-Brian

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 6:23pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/8 "2006-04-09T18:23:49Z")

</div>

David H.

> Also…when I try @params[“deal”][“utility”] I get an error for an  
> undefined method. Can’t evaluate nil.[]…???

In looking back at the controller code you posted, I don’t see that  
you’re  
passing the “deal” in. That seems to be the first problem this error  
message is getting you to look for. It may be that you just haven’t  
included that piece of code.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 6:37pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/9 "2006-04-09T18:37:42Z")

</div>

Brian H. '89 wrote:

> OK. This is a case where you need to have a view for this controller,  
> and in the view you place:
> 
> ```
> <%= params.debug %>
> ```
> 
> That will show you the makeup of the params object, so that you can  
> figure out how you need to reference the object in the hash.
> 
> If the HTML being produced shows a form field with name=“deal  
> [utility]”, that tells Rails to create a :deal hash, within the  
> params hash, with one entry using :utility as it’s key. Referencing  
> params[:deal][:utility] should return the value of that entry…
> 
> -Brian

Thanks to everyone for helping so far…Brian, when I tried your  
suggestion of params[:deal][:utility] this is the error I get:

NoMethodError in Deals#fill\_rec\_points  
You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.[]  
RAILS\_ROOT: ./script/…/config/…

Application Trace | Framework Trace | Full Trace  
#{RAILS\_ROOT}/app/controllers/deals\_controller.rb:27:in  
`fill\_rec\_points’

## Request Parameters: {“list\_item”=\>“SoCal”}

Response  
Headers: {“cookie”=\>[], “Cache-Control”=\>“no-cache”}

Any interpretation???

Thanks Again!!!

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 10:51pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/10 "2006-04-09T22:51:06Z")

</div>

> To “fix” this problem, you need to figure out why your form isn’t  
> properly submitting data to your controller when you hit the Submit  
> button. You are clicking on a submit button, aren’t you?
> 
> -Brian

Thanks for the help Brian (had to leave for a bit). Actually I am trying  
to grab this data while the user is entering information on a new record  
form, PRIOR to hitting submit. Like I said before, I have had this work  
in other Rails apps. This select control is located on the \_form  
partial which is embedded in the new.rhtml view of the  
deals\_controller.rb.

Could it be that it is not sending the deal hash because the record  
hasn’t been ‘saved’ yet. However, the ‘new’ method in the controller  
calls for Deal.new, so shouldn’t it have a hash already generated for  
the deal object, with the utility item?

My code is in the first post of this thread…tell me if you need more.  
Thanks…David.

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/11 "2006-04-09T19:19:28Z")

</div>

On Apr 9, 2006, at 12:37 PM, David H. wrote:

> Thanks to everyone for helping so far…Brian, when I tried your  
> suggestion of params[:deal][:utility] this is the error I get:
> 
> NoMethodError in Deals#fill\_rec\_points  
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occured while evaluating nil.[]
> 
> Request  
> Parameters: {“list\_item”=\>“SoCal”}

Right here is where we see what your problem is. For whatever reason,  
and I’m not sure what that reason is, since I haven’t seen your code,  
the params object that your controller is getting, doesn’t have a  
“deal” object… Without a “deal” object, requesting params[:deal] or  
params[“deal”] will return a nil object. This is how Ruby handles  
hash item access.

So, if you then try to reference params[:deal][:utility], you are  
asking Ruby to return the [:utility] item from a nil object, which it  
can’t do, so it throws an error. The last line of the error message,  
before the Parameters list, tells you that you are trying to do  
something with a nil object. That’s the other big clue in this error.

To “fix” this problem, you need to figure out why your form isn’t  
properly submitting data to your controller when you hit the Submit  
button. You are clicking on a submit button, aren’t you?

-Brian

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 9, 2006, 11:40pm UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/12 "2006-04-09T23:40:13Z")

</div>

On Apr 9, 2006, at 04:51 PM, David H. wrote:

> My code is in the first post of this thread…tell me if you need  
> more.  
> Thanks…David.

Actually, I hadn’t read the code very closely the first time, since I  
was only trying to answer the question of the proper way to refer to  
things inside the params hash object.

However, now that I’ve taken a look at your original post code, the  
answer jumped out at me. You define your observer as:

> \<%= observe\_field(“deal[utility]”,  
> :frequency =\> 0.25,  
> :update =\> “rec\_point\_list”,  
> :url =\> {:action =\> :fill\_rec\_points},  
> :with =\> “‘list\_item=’+value”) %\>

That :with tells the JavaScript how to send the value to your  
fill\_rec\_points method. You are sending the value through as  
“list\_item”, which is fine, but you’re not asking for “list\_item” out  
of the params hash. In fact, the error you posted showed us exactly  
that:

> Application Trace | Framework Trace | Full Trace  
> #{RAILS\_ROOT}/app/controllers/deals\_controller.rb:27:in  
> `fill\_rec\_points’
> 
> Request  
> Parameters: {“list\_item”=\>“SoCal”}

But, since I hadn’t looked at your original code that didn’t click  
for me when I first saw it. So, you can either reference params  
[:list\_item] (or params[“list\_item”] if you prefer), or you can  
modify your observe\_field :with to be ":with =\> “‘deal[utility]  
=’+value”, so that you’re current method of referencing params will  
work…

-Brian  
Application Trace | Framework Trace | Full Trace  
#{RAILS\_ROOT}/app/controllers/deals\_controller.rb:27:in  
`fill\_rec\_points’

Request  
Parameters: {“list\_item”=\>“SoCal”}

---

<div class="post-metadata">

**Author:** ![divotdave](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/bbe5ce/32.png) [@divotdave](https://www.ruby-forum.com/u/divotdave)\
**Post date:** [April 10, 2006, 12:02am UTC](https://www.ruby-forum.com/t/help-with-dependent-drop-down-and-params/57311/13 "2006-04-10T00:02:28Z")

</div>

> But, since I hadn’t looked at your original code that didn’t click  
> for me when I first saw it. So, you can either reference params  
> [:list\_item] (or params[“list\_item”] if you prefer), or you can  
> modify your observe\_field :with to be ":with =\> “‘deal[utility]  
> =’+value”, so that you’re current method of referencing params will  
> work…
> 
> -Brian

HALLELUJAH! It worked like a charm…I knew if someone looked at it they  
would see something I had overlooked, after pouring over it a hundred  
times.

Brian, I knew you had a good last name for a reason! Sweet!!!

Thanks dude, you rock…and thank you to everyone else who contributed  
to this thread. Taking time to help others is what OpenSource and Web  
2.0 is all about…what a great time to be alive!

Happy Railing!  
David
