2016-10-07 1 views
1

레일에서 선택한 레일을 사용하여 태그가있는 젬을 사용하여 포스트에 태그를 추가하는 기능을 구현했습니다. 존재하지 않는 사용자가 새 태그를 만들도록하여 기능을 더욱 향상시키고 싶습니다.레일에 레일이없는 경우 태그 추가

form.html.slim

.form-group.text-center 
     = f.label :tag_ids, "Tags" 
     = f.collection_select :tag_ids, Tag.order(:name), :id, :name, {}, {multiple: true} 

enter image description here

내가 존재하지 않는 경우 새 태그를 추가해야합니다. Selectize.js와 확인

+0

아약스 호출 또는 [중첩 속성] (http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html)을 사용하여이 작업을 수행 할 수 있습니다. 그러나 적어도 문제를 해결하려고 시도했거나 해결책을 들여다 보았다는 것을 보여 주어야합니다. 그렇지 않으면이 질문은 거의 닫힐 것입니다. – max

+0

며칠 전에 acts_as_taggable 및 selectize.js로이 작업을 수행했습니다. 그걸 사용하는 솔루션을 원한다면 알려주세요. – bkunzi01

+0

@ bkunzi01 어떻게 구현했는지 알게 될 것입니다. – maheshkumar

답변

2

, Acts_as_Taggable, & SimpleForm :

첫째, 일반 문자열 입력에 collection_select에서 입력을 변경합니다. Selectize.js는 모든 값을 쉼표가있는 모든 태그로 분리합니다. 이것은 사람들이 태그를 추가 할 때 장면 뒤에서 발생합니다. 실제로는 사용자가 제공 한 구분 기호를 사용하여 긴 문자열을 데이터베이스에 삽입합니다. 그런 다음 selectize.js 초기화

<%= f.input :nationalities, as: :string, placeholder: "Nationalities", input_html: { id: 'nationality-input'} %> 

: 당신은 같은 selectize을 초기화 할 수 있도록 그런 다음 해당 필드에 ID를 추가 할 필요가

#The following line gets all tags ever posted for a user with the context 'nationalities' which we will use as options. 
<%=nations = ActsAsTaggleOn::Tagging.includes(:tag).where(context: 'nationalities').uniq.pluck(:id, :name) 
$(document).ready(function() { 
$('#nationality-input).selectize({ 
    delimiter: ',', 
    persist: true, 
    allowEmptyOption: false, 
    options: [ 
     <% nations.each do |nat| %> 
     {text: '<%=nat[1] %>', value: '<%=nat[1]%>' }, 
     <% end %> 
    searchField: 'text', 
    create: function(input) { 
     return { 
      value: input, 
      text: input 
    } 
    } 
}); 
}); 

제대로 acts_as_taggable 설정되어 있는지 확인하고 해당 모델이 그것을 포함합니다. 그런 다음 컨트롤러에서 전체 문자열을 쉼표 및 모두로 저장하고 selectize를 사용하여보기에서 자동으로 다시 형식을 지정할 수 있습니다.