Pergunta

I'm getting the error "uninitialized constant Collection::CollectionComponent" from the check_box_tag at "@collection.components.include?" I'm not sure why this is happening as @collection seems to work fine in the form_for tag or if I remove the 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'

collection.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

component.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
Foi útil?

Solução

Rails is looking for a class with the name CollectionComponent but you only provide a class with the name CollectionComponents

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top