【问题标题】:Ruby on Rails has_and_belongs_to_many uninitialized constantRuby on Rails has_and_belongs_to_many 未初始化常量
【发布时间】:2014-02-19 16:14:14
【问题描述】:

我正在 Refinery CMS 上建立一个网站,并生成了两个扩展:一个用于 Brands,另一个用于 Bicycle Types(这是一个自行车商店的网站)。

现在,我想做的是让品牌扩展处理品牌页面的创建,这将被拉入品牌索引。在此页面上,我希望能够按 Bicycle Type 进行过滤,这是第二个扩展的来源。通过 Bicycle Type 扩展,您可以创建一个自行车类型,我想将其关联到一个品牌。一个品牌可以有多种自行车类型,反之亦然。

因此,我编辑了 Brands 模型以添加 has_and_belongs_to_many :bicycle_types,并编辑 Bicycle Types 模型以包括 has_and_belongs_to_many :brandsaccepts_nested_attributes_for :brands。我写了一个迁移来创建一个连接表,到目前为止一切都很顺利。

然后我去修改 Brands 扩展的表单,让我的复选框正确显示并且似乎生成了正确的代码。但是,当我提交表单时出现问题 - 我得到 NameError in Refinery::Brands::Admin::BrandsController#updateuninitialized constant Refinery::Brands::Brand::BicycleType

我得到的参数看起来像是正确传递了自行车类型 ID:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"3193ZMPXkmHdgZThXwAurD6xF2eZ533Tb71pAi7Jxbs=",
 "switch_locale"=>"en",
 "brand"=>{"title"=>"Cannondale",
 "teaser"=>"",
 "splash"=>"",
 "details"=>"",
 "introduction"=>"",
 "blockquote"=>"",
 "bicycle_type_ids"=>["1",
 "2"],
 "logo_id"=>"",
 "teaser_image_id"=>"",
 "splash_image_id"=>""},
 "id"=>"2",
 "locale"=>:en}

我一直在努力解决这个问题,只是不断地碰到同样的砖墙,所以任何帮助都将不胜感激!

这是我的代码。让我知道是否还有其他帮助。

品牌总监

module Refinery
  module Brands
    module Admin
      class BrandsController < ::Refinery::AdminController

        crudify :'refinery/brands/brand',
                :xhr_paging => true

      end
    end
  end
end

品牌模型

module Refinery
  module Brands
    class Brand < Refinery::Core::BaseModel
      self.table_name = 'refinery_brands'

      attr_accessible :title, :teaser, :splash, :details, :introduction, :blockquote, :logo_id, :teaser_image_id, :splash_image_id, :position, :bicycle_type_ids

      translates :title, :teaser, :splash, :details, :introduction, :blockquote

      class Translation
        attr_accessible :locale
      end

      validates :title, :presence => true, :uniqueness => true

      belongs_to :logo, :class_name => '::Refinery::Image'

      belongs_to :teaser_image, :class_name => '::Refinery::Image'

      belongs_to :splash_image, :class_name => '::Refinery::Image'

      has_and_belongs_to_many :bicycle_types
    end
  end
end

自行车类型模型

module Refinery
  module BicycleTypes
    class BicycleType < Refinery::Core::BaseModel
      self.table_name = 'refinery_bicycle_types'

      attr_accessible :title, :position

      translates :title

      class Translation
        attr_accessible :locale
      end

      validates :title, :presence => true, :uniqueness => true

      has_and_belongs_to_many :brands
      accepts_nested_attributes_for :brands
    end
  end
end

迁移

class AddRefineryBicycleTypesBrands < ActiveRecord::Migration
  def change
    create_table :bicycle_types_brands, :id => false do |t|
      t.references :bicycle_type
      t.references :brand
    end
    add_index :bicycle_types_brands, [:bicycle_type_id, :brand_id], :unique => true
  end
end

表单部分(至少是我构建复选框的部分)

<div class="field">
  <%= f.label :bicycle_types %>
    <% Refinery::BicycleTypes::BicycleType.order(:title).each do |bicycle_type| %>
      <label class="checkbox">
        <%= check_box_tag "#{f.object_name}[bicycle_type_ids][]", bicycle_type.id, f.object.bicycle_types %>
      <%= bicycle_type.title %>
    </label>
  <% end %>
</div>

如果部分的其余部分有用,或者其他任何与此相关的内容,请告诉我。任何帮助将不胜感激!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2 refinerycms has-and-belongs-to-many uninitialized-constant


    【解决方案1】:

    您必须指定完整的类名:

    has_and_belongs_to_many :bicycle_types, :class_name => "Refinery::BicycleTypes::BicycleType"
    

    这不是你的情况,但如果你想以“炼油厂风格”(即 refinery_bicycle_types_brands)调用连接表,你还必须声明连接表:

    has_and_belongs_to_many :bicycle_types, :join_table => :refinery_bicycle_types_brands, :class_name => "Refinery::BicycleTypes::BicycleType"
    

    再见

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多