Rails3-jquery-autocomplete,在 Rails 应用程序中的参数中传递对象 ID
我在带有 Mongoid 的 Rails 3.0.10 应用程序中使用rails3-jquery-autocomplete(版本rails3-jquery-autocomplete (1.0.2))。这颗宝石就像一个魅力,我只有一个小问题。将不胜感激任何解决它的帮助。
我基本上试图在参数中传递自动完成搜索结果的 id。目前,这就是我正在做的事情。
在控制器中:
class FeaturesController < ApplicationController
autocomplete :feature, :name
end
在视图中:
= autocomplete_field_tag :replacement_id, ' ', autocomplete_feature_name_features_path, :id_elements => '#replacement_id'
路由文件
resources :features do
get :autocomplete_feature_name, :on => :collection
end
自动完成工作正常。唯一的事情是,它传递的是文本,而不是传递自动完成对象的 id(使用 :id_elements => '#replacement_id')。
这是我在参数中得到的内容:
Parameters: {"feature"=>{"status"=>"Replaced"}, "replacement_id"=>"Property agreements", "commit"=>"update_status"}
当前“replacement_id”的值是“财产协议”,它是自动完成文本。
我一直在寻找相关问题。我发现在Github上的问题列表中,以前版本的gem也有类似的问题 此处和此处。已经解决了,所以我上面的设置肯定有问题。
非常感谢。
======更新======
哎呀!注意到视图助手中有一个拼写错误!它应该是 :id_element 而不是 :id_elements。在mo,这样做是有技巧的,即它在参数中传递对象id:
例如在参数中:
"replacement_id"=>"4e915ec88a812e2740000353"
但是它在自动完成文本框中插入对象id而不是自动完成文本。可能会找到解决办法。
======更新========
为了解决无法在文本框中显示自动完成文本的问题(目前它显示 id),我进行了以下更改:
查看:I使用 :update_elements 选项
= 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 是我放置在模型中的方法:
def replacement_name
"#{self.name}"
end
这样做会在自动完成文本框中显示文本,但现在选定的对象 ID 未在参数中传递,而是文本(如原来是这样)。
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 autocomplete 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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论如何,这一行
应该看起来像
这样,我在没有下一行的情况下在视图中使用了它:
可能您有不同的任务,但请尝试一下。
This line
should look probably like
anyway, I used this in view without next line:
may be you have a different task, but give it a try.