ruby-on-rails - 为什么 simple_form 用所有 comment_titles 填充这些选择列表?

标签 ruby-on-rails ruby ruby-on-rails-3 plugins simple-form

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

这是用于生成评论的表单,用户必须在其中选择所需的评论标题:

<%= 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_title 选择列表似乎使用已添加到所有视频的所有评论标题填充每个视频的每个列表,而不是仅使用已添加到该特定视频的 comment_titles 填充它。为什么会这样,我该如何解决?

最佳答案

也许你想要这样的东西?

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

关于ruby-on-rails - 为什么 simple_form 用所有 comment_titles 填充这些选择列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5561372/

相关文章:

mysql - 我可以按预定顺序加载 ActiveRecord-Objects 吗?

ruby-on-rails - 如何使用 gsub 删除 .在 ruby 字符串的末尾?

ruby-on-rails-3 - Rails 3.1 respond_to :html with :except

ruby-on-rails - 在Rails中,这种自定义Formtastic输入是否有更好的方法?

ruby-on-rails - Rails - 邮件,以纯文本形式获取正文

javascript - 如何在 Rails 中预填充动态添加的表单输入

ruby - Selenium::WebDriver::Error::StaleElementReferenceError 缓存中不存在元素

ruby - Ruby 数组与哈希测试中的堆栈级别太深

ruby-on-rails - 插件和 Ruby gem 之间的区别?

ruby - 开箱即用的 Redis-RB 在本地抛出 ECONNREFUSED 错误