我有一个应用程序,我在其中使用宝石rails3-jquery-autocomplete
和nested_form
建议现有用户被标记为项目条目的贡献者。Rails的jQuery自动完成和动态嵌套字段
表单视图:
<%= f.fields_for :contributors do |contributor|%>
<%= contributor.autocomplete_field :name, autocomplete_user_profile_last_name_projects_path, :update_elements => { :user_id => "#user_id" } %><br>
<%= contributor.hidden_field :user_id %>
<%= contributor.radio_button :contributor_type, 'Colleague' %>
<%= contributor.radio_button :contributor_type, 'Supervisor' %>
<%= contributor.radio_button :contributor_type, 'Client' %>
<%= contributor.link_to_remove "Remove this contributor" %>
<% end %>
<%= f.link_to_add "Add a contributor", :contributors %><br>
我也覆盖了get_autocomplete_function(parameters)
功能,以允许我自动完成多列,即列first_name
和last_name
def get_autocomplete_items(parameters)
items = UserProfile.select("DISTINCT CONCAT_WS(' ', first_name, last_name) AS full_name, first_name, last_name, id, user_id").where(["CONCAT_WS(' ', first_name, last_name) LIKE ?", "%#{parameters[:term]}%"])
end
我也有我的项目控制器中的以下行:
autocomplete :user_profile, :last_name, :full => true, :extra_data => [:first_name, :last_name, :user_id ], :display_value => :fullname
随着'全名”功能只是为:
def fullname
"#{first_name} #{last_name}"
end
我想要做的是,从自动完成的项目获得的user_id
值,并把隐藏字段中的值,但是当我检查服务器它会输出如下内容:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"DYQ7iJeqdwMQrprSGn0WNHXY5iXDZLA5pUd3OlYJ2so=", "project"=>{"creator"=>"1", "title"=>"lesdodis", "contributors_attributes"=>{"0"=>{"name"=>"Test User the 1st", "user_id"=>"", "contributor_type"=>"Client", "_destroy"=>"false", "id"=>"21"}, "1"=>{"name"=>"Test User the 2nd", "user_id"=>"", "contributor_type"=>"Supervisor", "_destroy"=>"false", "id"=>"22"}, "1393677266696"=>{"name"=>"", "user_id"=>"", "_destroy"=>"1"}, "1393677267518"=>{"name"=>"", "user_id"=>"", "_destroy"=>"1"}}, "description"=>"asdad", "tag_list"=>"sdads", "link"=>"asdasd", "gallery_attributes"=>{"screenshots_attributes"=>{"0"=>{"description"=>"", "_destroy"=>"false", "id"=>"10"}}, "id"=>"16"}}, "commit"=>"Update", "id"=>"16"}
其中user_id
字段内部的贡献者为空。我已经尝试通过使用contributor.object.id
为字段名称分配一个唯一编号,并且成功有限,因为新动态添加的字段没有对象标识,因此已尝试通过手动将ID分配给字段。我正在使用这个表单来创建和更新一个项目条目,所以我想请教如何使这项工作无论编辑该领域或添加一个新的帮助。
更新: 一个,而搜索的我终于得到它通过将此代码
<% field_name = "#{contributor.object_name}[user_id]".gsub(/(\])?\[/, "_").chop %>
<%= contributor.autocomplete_field :name, autocomplete_user_profile_last_name_projects_path, :update_elements => { :user_id => "##{field_name}" } %><br>
我希望这有助于人谁可能会遇到同样的问题,工作后。