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 回答