「@collection.components.include?」の check_box_tag から「uninitialized constant 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
collection_components.rb
class CollectionComponents < ActiveRecord::Base
attr_accessible :collection_id,
:component_id
belongs_to :collection
belongs_to :component
end