rails3-jquery-autocomplete, passing object id in params in Rails app
I am using rails3-jquery-autocomplete (version rails3-jquery-autocomplete (1.0.2)) in a Rails 3.0.10 app with Mongoid. The gem works like a charm, there's only one little issue I am having. Would appreciate any help in solving it.
I am basically trying to pass the id of an autocomplete search result in the params. Currently, this is what I'm doing.
In the controller:
class FeaturesController < ApplicationController
autocomplete :feature, :name
end
In the view:
= autocomplete_field_tag :replacement_id, ' ', autocomplete_feature_name_features_path, :id_elements => '#replacement_id'
The routes files
resources :features do
get :autocomplete_feature_name, :on => :collection
end
The autocomplete works fine. The only thing is that instead of passing the id of the automcompleted object (using :id_elements => '#replacement_id'), it passes the text.
This is what I get in the params:
Parameters: {"feature"=>{"status"=>"Replaced"}, "replacement_id"=>"Property agreements", "commit"=>"update_status"}
Currently the value of "replacement_id" is "Property agreements", which is the autocomplete text.
I have been searching around for related problems. I found that in the issues list on Github there had been similar problems in previous versions of the gem here and here. They have already been solved, so there must be something wrong with my setup above.
Many thanks.
======UPDATE======
oops! noticed a typo in the view helper! It should be :id_element instead of :id_elements. At the mo, doing so does the trick, i.e. it passes the object id in the params:
e.g. in the params:
"replacement_id"=>"4e915ec88a812e2740000353"
However it inserts the object id in the autocomplete text box instead of the autocompleted text. Might find a way around that.
======UPDATE========
To get around not being able to display the autocom开发者_运维知识库plete text in the text box (at the moment it shows the id), I have made the following changes:
View: I used the :update_elements option
= autocomplete_field_tag :replacement_id, '', autocomplete_feature_name_features_path, :update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}
Controller:
autocomplete :feature, :name, :display_value => :replacement_name, :extra_data => [:name]
:name is a method which I placed in the model:
def replacement_name
"#{self.name}"
end
Doing so displays the text in the autocomplete text box, but now the selected object id isn't passed in the params, the text is instead (as it was originally).
This line
= autocomplete_field_tag :replacement_id, '', autocomplete_feature_name_features_path, :update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}
should look probably like
= autocomplete_field_tag :replacement_id, '', controllername(ex: users)_autocomplete_feature_name_path, ....
anyway, I used this in view without next line:
:update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}
may be you have a different task, but give it a try.
精彩评论