Using Autocomplete With an Edit Page in Rails2

After pounding my head against the wall for quite some time trying to figure out how to get scriptaculous autocomplete working correctly in the app I am writing, I finally got it. If I were creating a new record, autocomplete would work perfectly, but when I went to edit that record, it did nothing. Looking at the log, it turned out that the id of the record being edited was being passed as the action, and the action was passed as the id.

Since I am fairly new to rails, I couldn’t figure out why it was getting passed like that. After doing a little digging, I found out that autocompleter was taking the action from the form and appending my_autocompleter_action_name to the end of it. So the request looked like this with new: /my_controller/my_autocompleter_action_name, but on edit it was: /my_controller/5/my_autocompleter_action_name (where 5 is the id of the record being edited).

Going into routes, the default route is :controller/:action/:id, so I let out a big duh and made a special route.

1
2
3
map.connect '/my_controller/:id/my_autocompleter_action_name', 
        :controller => 'my_controller', 
        :action => 'my_autocompleter_action_name'

Now it is working like a champ. Just thought I would share that for anybody else who is stuck on this one.

Leave a Reply