rails3-jquery-autocomplete,在Rails app中的params中传递对象id

时间:2011-10-07 02:03:53

标签: ruby-on-rails jquery-autocomplete

我在使用Mongoid的Rails 3.0.10应用程序中使用rails3-jquery-autocomplete(版本rails3-jquery-autocomplete(1.0.2))。宝石就像一个魅力,我只有一个小问题。非常感谢您解决它的任何帮助。

我基本上试图在params中传递自动完成搜索结果的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

自动填充功能正常。唯一的事情是,它不是传递automcompleted对象的id(使用:id_elements =&gt;'#replacement_id'),而是传递文本。

这是我在params中得到的:

Parameters: {"feature"=>{"status"=>"Replaced"}, "replacement_id"=>"Property agreements", "commit"=>"update_status"}

目前“replacement_id”的值是“属性协议”,即自动填充文本。

我一直在寻找相关的问题。我发现在Github的问题列表中,以前版本的gem herehere中存在类似的问题。它们已经解决了,所以上面的设置肯定有问题。

非常感谢。

====== UPDATE ==

糟糕!注意到视图帮助中的拼写错误!它应该是:id_element而不是:id_elements。在mo,这样做就是技巧,即它传递了参数中的对象id:

e.g。在参数中:

"replacement_id"=>"4e915ec88a812e2740000353"

但是,它会在自动填充文本框中插入对象ID,而不是自动填充的文本。可能会找到解决方法。

== UPDATE ======

为了避免在文本框中显示自动填充文本(此时它显示了ID),我做了以下更改:

查看:我使用了:update_elements选项

= autocomplete_field_tag :replacement_id, '', autocomplete_feature_name_features_path, :update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}

控制器:

autocomplete :feature, :name, :display_value => :replacement_name, :extra_data => [:name]

:name是我放置在模型中的方法:

def replacement_name
 "#{self.name}"
end

这样做会在自动填充文本框中显示文本,但是现在所选的对象ID不会在参数中传递,而是文本(就像它原来的那样)。

1 个答案:

答案 0 :(得分:0)

这一行

  = autocomplete_field_tag :replacement_id, '', autocomplete_feature_name_features_path, :update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}

应该看起来像

= autocomplete_field_tag :replacement_id, '', controllername(ex: users)_autocomplete_feature_name_path, ....

无论如何,我在没有下一行的情况下使用了这个:

 :update_elements => {:id => '#input#replacement_id', :name => 'input#replacement_id'}

可能你有不同的任务,但试一试。