导轨|同一列的形式多次| has_many通过

时间:2017-03-16 13:38:21

标签: ruby-on-rails simple-form has-many-through wicked-gem

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

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值是subtopics。

这是表格。

<%= 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

0 个答案:

没有答案