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 Answer 1

1

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') %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks; I've made the change. However, my controller function still isn't being called. Do I have any other syntax problems?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.