Help With Dependent Drop-Down and @Params


#1

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

the other posted by Adam at
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…


#2

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
To: 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

the other posted by Adam at

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


#3

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…


#4

@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…


#5

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.[]…???


#6

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…


#7

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


#8

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


#9

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!!!


#10

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.


#11

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


#12

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”}


#13

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