0

モデルにタグを追加しようとしています。私はそれが次の機能で機能する必要があります。select2、simple_form、acts_as_taggable_on

ビュー内のタグとしてレンダリング
  • SELECT2を使用して、ユーザーが独自のタグを作成することができます(。すなわち、その後「スペース」や、新しいタグを入力してください「」)
  • 入力を一貫して保つために既存のタグを入力するように求められます

質問を投稿するときにこのサイトで同じように「タグ」ボックスが機能します。

私はそこに行く途中で99%ですが、私が取るアプローチはそれぞれ1つの部分がうまくいかないようです。

  • SELECT2レール(3.5.7)
  • simple_form(3.0.1)
  • 行為-AS-タグ付けできるオン(4.0.0)
  • :ここ

    は、私のソフトウェアのバージョンです次のコード

  • Railsの4.03

:正しくレンダリングが、保存する前に、新しいタグを追加することはできませんが、既存のタグの入力を求める

の作品
@tags = ActsAsTaggableOn::Tag.all 
<%= f.input :tag_list, input_html: { class: 'tags', multiple: true}, collection: @tags, value_method: :name %> 

$('.tags').select2({ 

    tokenSeparators: [',', ' '], 
    placeholder: "Add your tag here" 
}); 

次のコード:正しくレンダリング、および新しいタグを追加することができますが、既存のタグ

を選択するためのプロンプトではない:次のコードSELECT2

@tags = ActsAsTaggableOn::Tag.all 
<%= f.input :tag_list, input_html: { class: 'tags', multiple: true}, collection: @tags, value_method: :name %> 

$('.tags').select2({ 
    tags: true, 
    tokenSeparators: [',', ' '], 
    placeholder: "Add your tag here" 
}); 

を経由して、複数行のテキストボックスではなく、タグとしてレンダリング

@tags = ActsAsTaggableOn::Tag.all 
<%= f.input :tag_list, input_html: { class: 'tags', multiple: true } %> 

$('.tags').select2({ 
    tags: true, 
    tokenSeparators: [',', ' '], 
    placeholder: "Add your tag here" 
}); 

答えて

0

これを見つけた人のために、私はこれをやって仕事をしました。

@tags = ActsAsTaggableOn::Tag.all.pluck(:name) 
<%= f.input :tag_list, input_html: { class: 'tags', multiple: true, data: {options: @tags} } %> 

$('.tags').select2({ 
    tags: $('.tags').data('options'), 
    tokenSeparators: [' ', ','] 
}); 

クリーンな方法があれば幸いです。

関連する問題