1

我有一个创建帖子及其主题的多步骤表单。

class Post < ActiveRecord::Base
  has_many :post_topics
  has_many :topics, through: :post_topics
end

class Topic < ActiveRecord::Base
  has_many :post_topics
  has_many :posts, through: :post_topics
  belongs_to :parent, class_name: 'Topic'
  has_many :subtopics, class_name: 'Topic', foreign_key: 'parent_id'

  scope :subtopics, lambda { where.not(parent_id: nil) }
  scope :topics, lambda { where(parent_id: nil) }
end

class PostTopic < ActiveRecord::Base
  belongs_to :post
  belongs_to :topic
end

主题类具有自引用关联。我parent_id在主题表中有一个列。具有 parent_id: nil 的主题是根主题,具有 parent_id 值的主题是子主题。

这是表格。

<%= simple_form_for(@post, method: :put, url: wizard_path) do |f| %>
 <%= f.error_notification %>

 <%= f.input :name, required: true, autofocus: true %>
 <%= f.input :description, required: true %>
 <%#= f.select :topic_ids, Topic.topics.collect {|x| [x.name, x.id]}, {}, label: "Select Topic" %>  
 <%= f.select :topic_ids, Topic.subtopics.collect {|x| [x.name, x.id]}, {}, :multiple => true, class: "multipleSelect", label: "Select Subtopic" %>
 <%= f.button :submit, "NEXT", class: 'btn btn-danger' %>

<% end %>

表格看起来像这样

Name
Description
Select SubTopics

名称、描述和选择子主题字段按预期保存值。我想要的是另一个选择输入,它允许用户选择一个主题(父主题)(在上面的表格中注释掉该行)

Name
Description
Select Topic
Select SubTopics

如何以这种形式多次保留同一列(topic_ids),以便

"Select Topic" input will save the parent topic
"Select Subtopics" input will save multiple subtopics
4

0 回答 0