为什么 simple_form 用所有 comment_title 填充这些选择列表?

发布于 2024-10-30 01:40:57 字数 1473 浏览 1 评论 0原文

所以我有这个用于生成 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 %>

现在问题是由 <%= f.association :comment_title, :label => 生成的 comment_title 选择列表"评论标题:", :include_blank => false %> 似乎使用已添加到所有视频的所有评论标题填充每个视频的每个列表,而不是仅使用已添加到该特定视频的 comment_titles 填充它。这是为什么?我该如何解决它?

So i have this form for generating 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 %>

And this is the relevant part of my video model that allows comment_titles to be created as a virtual attribute:

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

Then this is the form for generating a comment where a user must select the desired 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 %>

Now the problem is that the comment_title select lists generated by <%= f.association :comment_title, :label => "Comment Title:", :include_blank => false %> seems to populate each list for each video with ALL of the comment titles that have been added to all videos instead of only populating it with the comment_titles that have been added to that specific video. Why is this and how can I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨夜星沙 2024-11-06 01:40:57

也许你想要这样的东西?

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

Maybe you want something like this?

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

临走之时 2024-11-06 01:40:57

默认情况下 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, ...

By default simple_form don't use the scope of association. See how it works here: https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/form_builder.rb#L130

Pass the collection option for the association like this:

f.association :comment_title, :collection => @video.comment_titles, ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文