나는 rails3-jquery-autocomplete
및 nested_form
을 사용하여 기존 사용자가 프로젝트 항목에 참여자로 태그 지정되도록 제안하는 애플리케이션이 있습니다.레일 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>
나는 또한 나의 자동 완성에 하나 이상의 열, 즉 열 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
나는 또한이 수 있도록하기 위해 get_autocomplete_function(parameters)
기능을 덮어했다 내 프로젝트 컨트롤러의 다음 줄 :
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
의 값을 취득하고 숨겨진 필드 내에서 그 값을 넣어하는 것입니다,하지만 난 Server 사용을 검사 할 때
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
필드의 내용은 공백으로 남겨 둡니다. 나는 수동으로 필드에 을 사용하여 동적으로 추가 된 새로운 필드에 객체 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>
나는이 같은 문제가 발생할 수 있습니다 사람을 도움이 되었으면 좋겠를 추가하여 작동하도록있어 검색의 잠시 후 .