Ruby on rails 使用复选框标记的未初始化常量有很多:尽管

Ruby on rails 使用复选框标记的未初始化常量有很多:尽管,ruby-on-rails,checkbox,has-many-through,uninitialized-constant,Ruby On Rails,Checkbox,Has Many Through,Uninitialized Constant,我从“@Collection.components.include?”的复选框标记中得到错误“uninitialized constant Collection::CollectionComponent”,我不确定为什么会发生这种情况,因为@Collection在标签的形式中似乎工作得很好,或者我是否删除了复选框标记 \u form.html.haml = form_for @collection do |f| - if @collection.errors.any? #error_

我从“@Collection.components.include?”的复选框标记中得到错误“uninitialized constant Collection::CollectionComponent”,我不确定为什么会发生这种情况,因为@Collection在标签的形式中似乎工作得很好,或者我是否删除了复选框标记

\u 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
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
class CollectionComponents < ActiveRecord::Base
  attr_accessible :collection_id, 
                  :component_id

  belongs_to      :collection
  belongs_to      :component
end
类集合:销毁
有多个:组件,:到=>:集合\u组件
属于:风格
验证是否存在:样式
验证是否存在:title
保存前:创建\u名称
私有的
def创建_名称
self.name=title.parameterize
结束
结束
组件.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
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
class CollectionComponents < ActiveRecord::Base
  attr_accessible :collection_id, 
                  :component_id

  belongs_to      :collection
  belongs_to      :component
end
class组件:销毁
有多个:集合,:到=>:集合\u组件
有很多:组件样式
有许多:样式,:至=>:组件\u样式
属于:类别
验证是否存在:类别
验证是否存在:title
保存前:创建\u名称
私有的
def创建_名称
self.name=title.parameterize
结束
结束
收集组件。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
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
class CollectionComponents < ActiveRecord::Base
  attr_accessible :collection_id, 
                  :component_id

  belongs_to      :collection
  belongs_to      :component
end
class CollectionComponents
Rails正在寻找一个名为
CollectionComponent
的类,但您只提供了一个名为
CollectionComponents
的类,为什么我使用了“CollectionComponents”时它在寻找“CollectionComponent”在对模型的所有引用中,这是关于Rails需要了解的事情之一:当您使用复数形式,如“has\u many:component\u styles”时,Rails假定其名称为singluar。因为它更自然:“它有许多组件样式,它们由类ComponentStyle的对象组成”这是有道理的。我将模型“CollectionComponents”更改为“CollectionComponent”,现在收到一个DB错误:ActiveRecord::StatementInvalid-PG::error:错误:列引用“id”不明确第1行:…on_id”为NULL,“components”。“id”“=1按id ASC LIM排序的订单^我猜这是因为我一开始就错误地生成了模型?我不理解你的对象设计,所以我不能帮你。。看来你应该有一个has_并且属于许多协会,也许读一下这篇文章会有所帮助?事实上,在我看来,他们使用的例子正是你所需要的。