我从“@collection.components.include?”的 check_box_tag 中收到错误“未初始化的常量 Collection::CollectionComponent”。我不确定为什么会发生这种情况,因为@collection 在 form_for 标记中似乎可以正常工作,或者如果我删除了 check_box_tag。
_form.html.haml
= form_for @collection do |f|
- if @collection.errors.any?
#error_explanation
%h1= "#{pluralize(@collection.errors.count, "error")} prohibited this collection from being saved:"
%ul
- @collection.errors.full_messages.each do |msg|
%li= msg
.field
- Component.all.each do |component|
= label_tag :component_ids, component.id
= check_box_tag :component_ids, component.id, @collection.components.include?(component), :name => 'collection[component_ids][]'
.field
= f.label :title
= f.text_field :title
.actions
= f.submit 'Save'
集合.rb
class Collection < ActiveRecord::Base
default_scope order('id ASC')
attr_accessible :style_id,
:name,
:title,
:component
has_many :collection_components, :dependent => :destroy
has_many :components, :through => :collection_components
belongs_to :style
validates_presence_of :style
validates_presence_of :title
before_save :create_name
private
def create_name
self.name = title.parameterize
end
end
组件.rb
class Component < ActiveRecord::Base
default_scope order('id ASC')
attr_accessible :category_id,
:name,
:title,
:collection,
:style
has_many :collection_components, :dependent => :destroy
has_many :collections, :through => :collection_components
has_many :component_styles
has_many :styles, :through => :component_styles
belongs_to :category
validates_presence_of :category
validates_presence_of :title
before_save :create_name
private
def create_name
self.name = title.parameterize
end
end
集合组件.rb
class CollectionComponents < ActiveRecord::Base
attr_accessible :collection_id,
:component_id
belongs_to :collection
belongs_to :component
end