# Dynamic drop-downs in a form\_for using AJAX remote\_function - Help

**URL:** <https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548>\
**Category:** Rails\
**Created:** [August 11, 2009, 9:52am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548 "2009-08-11T09:52:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 11, 2009, 9:52am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/1 "2009-08-11T09:52:57Z")

</div>

Hello -

I am fairly new to Ruby on Rails, but feel like I am learning quick.  
I have what seems to be a fairly unique issue as I cannot find much  
out there that describes what I’m seeing. Hopefully it’s a very  
simple fix, and I simply can’t see the forest through all the trees!

I am attempting to create 2 related drop-down lists in the same  
form\_for, both using collection\_select.  
On the first drop-down, I have an onChange “remote\_function” call that  
I want to send an AJAX call to the controller and filter the list in  
the 2nd drop-down based on what was selected in the first. In the  
controller method, I then call page.replace\_html to render a partial.  
In the partial is the “updated/filtered” 2nd collection\_select drop-  
down list.

The remote\_function works fine and calls the Controller method when  
the first drop-down changes. I am also able to pass the selected value  
to the method and filter the records returned. The problem occurs  
when the controller attempts to render the partial, I get the  
following error:

ActionView::TemplateError (undefined local variable or method `f’ for  
#ActionView::Base:0xb6e7b2f4) on line #1 of app/views/listings/  
\_automodels.html.erb:  
\<%= f.collection\_select :model, @automodels, :model, :model, {:prompt  
=\> “-Select a Make First-”} %\>

This leads me to believe that the “f” variable the rest of the fields  
in the form\_for are using is not available to the newly-replaced  
“collection\_select”, but I’m not sure why?

If the partial contains a simple HTML instead of the  
collection\_select, the replace\_html works fine.

Source code is worth 1,000 words, so here it is:

listings/new.html.erb

1. \<% form\_for([@user, @listing]) do |f| %\>
2. \<%= f.error\_messages %\>
3. 

4. 

```
<%= f.label :make %><br />

```

5. 

```
<%= f.collection_select :make, @automakes, :make, :make,

```

{:prompt =\> “-Select a Make-”}, {:onChange =\> remote\_function(:url =\>  
{:action =\> ‘get\_automodels’}, :with =\> “‘make=’ + this.value”)} %\>  
6.

  
7. 

8. \<%= f.label “Model:” %\>  
  
9.

  
10. \<%= f.collection\_select :model,  
@automodels, :model, :model, {:prompt =\> “-Select a Make First-”} %\>  
11. 
  
12.   
13. 

14. \<%= f.submit ‘Create’ %\>  
15.

  
16. \<% end %\>

listings\_controller.rb

1. def get\_automodels
2. @automodels = Automodels.find\_by\_make(params[:make])
3. render :update do |page|
4. 

```
page.replace_html('automodelsdiv', :partial => 'automodels')

```

5. end
6. end

\_automodels.html.erb

1. \<%= f.collection\_select :model, @automodels, :model, :model,  
{:prompt =\> “-Select a Make First-”} %\>

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 11, 2009, 9:55am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/2 "2009-08-11T09:55:18Z")

</div>

On Aug 11, 2:50 am, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> “collection\_select”, but I’m not sure why?  
> Because that f referred to a form builder object created by form\_for.  
> You’re in a different scope to when you rendered the original form, in  
> a different request, with a different instance of the controller,  
> possibly on a different mongrel or even a different server. The form  
> builder isn’t going to magically appear.

Fred

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 12, 2009, 4:21am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/3 "2009-08-12T04:21:10Z")

</div>

Thanks Frederick for looking at my issue.

Apparently I don’t have a full understanding of how the AJAX request  
interacts with (or I guess doesn’t interact with) the form\_for…  
So is the basic idea of what I’m attempting completely wrong or is  
there some slight modification I can do to pull it off?  
Basically, I have a form where I want to create a new listing record -  
but I want the fields to be restricted to values stored in the  
automodels Model in the database (and the model drop-down to be  
filtered based on the make selected).  
From what I’ve been reading, the best way to do the “dynamic select”  
is using AJAX. However, I guess the collection\_selects are never  
inside a form in the examples I’ve read?

I’m open to any suggestions from the experts here - even if it means  
overhauling the whole thing… 🙂

On Aug 11, 3:54 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 12, 2009, 5:22am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/4 "2009-08-12T05:22:36Z")

</div>

You could put each select in a partial then re-render it when coming  
back from the rjs with the updated select items.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 12, 2009, 9:51am UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/5 "2009-08-12T09:51:18Z")

</div>

On Aug 12, 3:20 am, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks Frederick for looking at my issue.
> 
> Apparently I don’t have a full understanding of how the AJAX request  
> interacts with (or I guess doesn’t interact with) the form\_for…

Forget about the ajaxness of it. If you had a page with a form and you  
followed a link from that page you wouldn’t expect the form builder  
from the first page to be hanging around when you render the second  
page. If you want to use the form builder version of a helper you just  
need to create a new form builder for the template that gets rendered  
for the ajax request (you can use fields\_for to create a form builder  
without actually creating tags)

Fred

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [August 31, 2009, 9:07pm UTC](https://www.ruby-forum.com/t/dynamic-drop-downs-in-a-form-for-using-ajax-remote-function-help/173548/6 "2009-08-31T21:07:18Z")

</div>

Hi  
I am also struck at the same point please help me … same problem u r  
facing  
sandeep
