「投稿」オブジェクト (ブログ投稿など) の単純なフォームがあり、フォーム フィールドの 1 つは「タグ」コレクション用です。「タグ」は、レールのデフォルトである「名前」以外に 1 つのフィールドのみを持つ has_and_belongs_to_many 関連付けです。
ユーザーにタグを入力してもらい、それらを既存のタグと照合してから、post オブジェクトを保存する前に、first_or_create を使用してコントローラーの「タグ」配列に保存します。
これを設計する方法が本当にわかりません。最善のアプローチは何ですか?
「投稿」フォーム:
<%= simple_form_for @post, :html => { :class => 'form-horizontal' } do |f| %>
<%= f.input :shared_url, :required => false, input_html: { class: 'span6' } %>
<%= f.input :title, :required => false, input_html: { class: 'span6' } %>
<%= f.input :content, as: :text, :required => false, input_html: { class: 'span6' } %>
<%= f.input :tags, :required => false, input_html: { class: 'span6' } %>
<div class="form-actions">
<%= f.button :submit, "Post", :class => 'btn btn-primary btn-large' %>
</div>
<% end %>