1

だから私はcomment_titlesを生成するためのこのフォームを持っています:

<%= simple_form_for @video, :remote => true do |f| %>
    <%= f.input :comment_title_names, :label => false, :placeholder => "Add a Comments Title" %>
    <%= f.button :submit, :value => 'Add', :id => 'add_comment_title' %>
    <div class='hint'>Let your listeners know what comments you want by adding a guiding title for them. Pose a question, ask for feedback, or anything else!</div>
<% end %>

これは、comment_titles を仮想属性として作成できるようにする、ビデオ モデルの関連部分です。

attr_accessor :comment_title_names
after_save :assign_comment_titles

def assign_comment_titles
  if @comment_title_names
    self.comment_titles << @comment_title_names.map do |title|
      CommentTitle.find_or_create_by_title(title)
    end
  end
end

次に、これは、ユーザーが目的の comment_title を選択する必要があるコメントを生成するためのフォームです。

<%= simple_form_for([@video, @video.comments.new]) do |f| %>
  <%= f.association :comment_title, :label => "Comment Title:", :include_blank => false %>
  <%= f.input :body, :label => false, :placeholder => "Post a comment." %>
  <%= f.button :submit, :value => "Post" %>
<% end %>

問題は、によって生成された comment_title 選択リストが<%= f.association :comment_title, :label => "Comment Title:", :include_blank => false %> 、その特定のビデオに追加された comment_titles だけを入力するのではなく、すべてのビデオに追加されたすべてのコメント タイトルを各ビデオの各リストに入力するように見えることです。これはなぜですか、どうすれば修正できますか?

4

2 に答える 2

4

多分あなたはこのようなものが欲しいですか?

f.association :comment_title, :collection => @video.comment_titles, ...

于 2011-04-06T04:59:42.717 に答える
0

デフォルトでは、simple_form は関連付けのスコープを使用しません。ここでどのように機能するかを参照してください: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L130

次のように、関連付けのコレクション オプションを渡します。

f.association :comment_title, :collection => @video.comment_titles, ...
于 2011-04-07T13:30:53.590 に答える