views:

159

answers:

1

I have a form field where a user enters contact information, including name, email, etc. If they already have contacts saved I want to have a dropdown with their saved contacts. When a contact from the dropdown is selected, the contact fields should be populated with that data to allow for easy editing.

For some reason this approach isn't working:

In my new.html.erb view:

<%= f.collection_select :id, @contacts, :id, :name, :onchange =>
remote_function(:url =>{:action => 'populate_contact_form'}, :with => 'id') %>

In my controller:

def populate_contact_form
raise "I am working up to this point"
@contact = current_account.contacts.find(params[:id])
end

In populate_contact_form.rjs:

page['contact_name'].value = @contact.name
page['contact_email'].value = @contact.email

It seems my controller method is never called... can anyone explain how to do this?

+1  A: 

It's not getting called because you're using remote_function incorrectly. Rails assumes the current action/controller/id/etc if any of those options are missing from the :url option to remote_function. You're passing :action as a top level option to remote_function, and it gets ignored. With a :url option Rails assumes the same action and controller that rendered this view.

This should fix your problem:

<%= f.collection_select :id, @contacts, :id, :name, :onchange => 
  remote_function(:url =>{:action => 'populate_contact_form'}, :with => 'id') %>
EmFi
Thanks; I've made the change. However, my controller function still isn't being called. Do I have any other syntax problems?
Violet
All that's left to do is follow the logs. Both Firebug and your server log will capture the error returned by the server/javascript.
EmFi