Newb q: Dynamic Dropdown Menu - Observe field - Ajax

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

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 ([email protected]) %> <%= @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

On Aug 31, 1:19 pm, Adam T. [email protected]
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

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

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

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

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!