# Newb q: Dynamic Dropdown Menu - Observe field - Ajax

**URL:** <https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336>\
**Category:** Rails\
**Created:** [August 31, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336 "2008-08-31T14:19:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [August 31, 2008, 2:19pm UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336/1 "2008-08-31T14:19:01Z")

</div>

Hi Guys,

I am trying to get my head around drop down menus and observe\_field  
(ajax?)

I have :  
a ‘job’ model  
a vfxshotid model

A job has\_many :vfxshotids  
A vfxshotid belongs\_to :job

I need a page that has 2 drop down menus  
The first - list of ‘vfxshotids’ for a job  
The second - is a list of directories in a ‘vfxshotid’ directory.

At the moment I don’t know how to pass the arguements around. I followed  
this post but i can get it to work, and i wonder if the code is out of  
date anyway -  
[http://www.nabble.com/related-drop-down-list---ajax-td2367869.html](http://www.nabble.com/related-drop-down-list---ajax-td2367869.html)

If anyone can give me some advice I’d really appreciate it.

Thanks!

Adam

Here is my code:

* * *

## VIEW - newvfxasset.html.erb

\<% title ‘New Asset’%\>

# New asset

\<%= error\_messages\_for :asset %\>

\<% form\_for @asset do |f| %\>

 Select VFX shot id \<% for vfxshot in (removed\_email\_address@domain.invalid) %\> \<%= @job.vfxshotids[vfxshot].jobvfxshot\_name %\> \<% end %\> 
 Select VFX app 

\<%= observe\_field(“vfxshotid”, :frequency =\> 0.25, :update =\>  
“vfxapp\_id\_container”, :url =\> { :action =\> :get\_vfxapps\_for\_vfxshotid  
}, :with =\> “‘vfxshotid=’+value”) %\>

\<%= f.submit "Create" %\>

\<% end %\>

\<%= link\_to ‘Back’, assets\_path %\>

* * *

## jobs controller

def newvfxasset

```
def get_vfxapps_for_vfxshotid

    @loggedinuser = session[:user_id]
    @assetstartpath = @@jobsrootdirectory + '/' + @job.jobidstring +

```

‘\_’ + @job.client.name + ‘/vfx/’ + vfxshotid + ‘/’ + @loggedinuser.login

```
    print @assetstartpath
    # @vfxshotidvfxapps = [Dir.glob(@assetstartpath)]
    @vfxshotidvfxpaps = ['a', 'b', 'c', 'd']

end

@job = Job.find(params[:id])

@asset = Asset.new

respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @job }
end

```

end

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [August 31, 2008, 2:54pm UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336/2 "2008-08-31T14:54:43Z")

</div>

On Aug 31, 1:19 pm, Adam T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi Guys,
> 
> I am trying to get my head around drop down menus and observe\_field  
> (ajax?)  
> Yup observe\_field uses ajaz

> At the moment I don’t know how to pass the arguements around. I followed  
> this post but i can get it to work, and i wonder if the code is out of  
> date anyway -[http://www.nabble.com/related-drop-down-list---ajax-td2367869.html](http://www.nabble.com/related-drop-down-list---ajax-td2367869.html)

Well something that will confuse the browser is the fact that you’ve  
got 2 things on the page with id vfxapp\_id\_container which can easily  
have unpredictable consequences. Your get\_vfxapps\_for\_vfxshotid action  
should be using params[:vfxshotid] since that’s what will get  
submitted by observe\_field. Other than that you haven’t said exactly  
what is happening/not working so it’s hard to say anything more.

Fred

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [September 1, 2008, 4:16am UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336/3 "2008-09-01T04:16:42Z")

</div>

Fred thanks for replying - I will try to apply what you’ve suggested and  
post the results,  
cheers!

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [September 2, 2008, 9:42am UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336/4 "2008-09-02T09:42:12Z")

</div>

Hi Fred,

I think I have made some progress - i think my arguments get passed into  
a function in my controller - but i just can’t seem to get them to  
appear in the 2nd drop down menu - although the menu does seem to  
refresh (but is rendered blank).

Mind taking a look?  
Any pointers would be great!  
Thanks  
Adam

* * *

## controller

def get\_vfxapps\_for\_vfxshotid

```
  @job = Vfxshotid.find(params[:id]).job
  @loggedinuser_login = User.find(session[:user_id]).login

  puts 'job: ' + @job.jobidstring
  puts 'user: ' + @loggedinuser_login
  @vfxshotid = Vfxshotid.find(params[:id]).jobvfxshot_name

  @assetstartpath = @@jobsrootdirectory + '/' + @job.jobidstring + 

```

‘\_’ + @job.client.name + ‘/vfx’ + ‘/’ + @vfxshotid + ‘/work/’ +  
@loggedinuser\_login.to\_s  
@vfxshotidvfxapps = Dir.glob(@assetstartpath + “/_/_”)

```
  #see in terminal if directories are found
  puts 'path: ' + @assetstartpath
  for dir in @vfxshotidvfxapps
    puts dir
  end

```

end

* * *

## view - newvfxasset.html.erb

\<% title ‘New Asset’%\>

# New asset

\<%= error\_messages\_for :asset %\>

\<% form\_for @asset do |f| %\>

 \<%= f.collection\_select :job\_id, @job.vfxshotids, :id, :jobvfxshot\_name, :prompt =\> "Select your VFX Shot ID" %\> Select your VFX App 

```
<%= observe_field("vfxshotid",
:url => { :action => "get_vfxapps_for_vfxshotid" },
  :update => "vfxshotidapps",
:with => "'id=' + value",
:on => "changed")
%>

<%= f.submit "Create" %>

```

\<% end %\>

\<%= link\_to ‘Back’, assets\_path %\>

* * *

## view - get\_vfxapps\_for\_vfxshotid.html.erb

\<% for dir in @vfxshotidvfxapps %\>  
\<%= dir %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![toro04](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ea5d25/32.png) [@toro04](https://www.ruby-forum.com/u/toro04)\
**Post date:** [September 2, 2008, 10:51am UTC](https://www.ruby-forum.com/t/newb-q-dynamic-dropdown-menu-observe-field-ajax/147336/5 "2008-09-02T10:51:34Z")

</div>

haha! success!

the controller’s function seemed to be outputting with a layout - ???

i read that to disable a rendered layout u just need to append the  
following to a function:

render :layout =\> false

sweet!
