views:

31

answers:

1

Hello,

I just started working on a Ruby On Rails project and i got to the point where i need to see the contacts of a company.

They should appear once the company is selected..

<p>
    <%= f.label :empresa_id %><br />
    <%= f.select(:empresa_id, @empresa.map {|e| [e.nombre, e.id]} )%>
</p>

<%= observe_field  :empresa_id, :url=>{:action => "get_contactos", 
     :controller=> :contactos, :updatewith =>:empresa_id} %>

But nothing happens, i don't see even a error on the script/server.

Can someone point me in the right direction?

http://nealenssle.com/blog/2007/04/12/how-to-dynamically-update-form-elements-in-rails-using-ajax/

I see on the link that a guy did the exact same thing i need but did not post any info.

Cheers.

A: 

Kinda got this working.. here's my update..

This is the form were my select boxes are placed:

<% for e in @empresa %> "><%= e.nombre %> <% end %>

<%= observe_field "empresa_id", :update => "contacto_id", :with => "empresa_id", :url => { :controller => "contactos", :action => "get_contactos" } %>

The function get_contactos on the contactos controller :

def get_contactos @contacto = Contacto.find_all_by_empresa_id(params[:empresa_id]) render :layout => false end

And the view get_contactos.rhtml (contactos):

<% for c in @contacto %> "><%= c.nombre + ' ' + c.apellido%>%> <% end %>

I just need to bind the form to the correct fields and the job will be finished.

I couldn't get this to work by observing the field :empresa_id but "empresa_id" works..

Edit: Got it to work.

I just binded the field on the form to the proper ones on the model and now i have the data =)

<% for e in @empresa %> "><%= e.nombre %> <% end %>

<%= observe_field "empresa_id", :update => "llamada_contacto_id", :with => "empresa_id", :url => { :controller => "contactos", :action => "get_contactos" } %>

ZeroSoul13