2014-03-01 77 views
2

我有一个应用程序,我在其中使用宝石rails3-jquery-autocompletenested_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_namelast_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> 

我希望这有助于人谁可能会遇到同样的问题,工作后。

回答

1

我一直在摔跤类似的问题。在你的情况下,我的感觉是,nested_form助手正在生成唯一的标识符,这是击败传递给:update_elements的“#user_id”选择器。

您可能可以通过挂钩每个gem的事件流来解决该问题,然后手动更新表单中特定贡献者的隐藏:user_id。对于动态添加贡献者,可能是这样的:(即编辑)

$(document).on('nested:fieldAdded', function(nf_event) { 
    var ac_input = nf_event.field.find('.ui-autocomplete-input'); 
    ac_input.bind('railsAutocomplete.select', function(ac_event, data) { 
    $(this).next('input[type=hidden]').val(data.item.id); 
    }); 
}); 

对于通过现有关联产生的贡献者,可以绑定自己的自动完成输入上的$(document)。就绪()。这至少可以让你指出正确的方向。

1

后搜索的同时,我终于得到它通过添加该代码

<% 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}" } %> 

我希望这有助于人谁可能会遇到同样的问题的工作。