我正在 Refinery CMS 上建立一个站点,并生成了两个扩展:一个用于Brands,另一个用于Bicycle Types(这是一个自行车商店的站点)。
现在,我想做的是让 Brands 扩展处理品牌页面的创建,这将被拉入品牌索引。在此页面上,我希望能够按自行车类型进行过滤,这是第二个扩展的来源。通过自行车类型扩展,您可以创建一个自行车类型,我想将其关联到一个品牌。一个品牌可以有多种自行车类型,反之亦然。
因此,我编辑了 Brands 模型以添加has_and_belongs_to_many :bicycle_types
,并编辑了 Bicycle Types 模型以包含has_and_belongs_to_many :brands
和accepts_nested_attributes_for :brands
。我写了一个迁移来创建一个连接表,到目前为止一切都很顺利。
然后我去修改 Brands 扩展的表单,让我的复选框正确显示并且似乎生成了正确的代码。但是,当我提交表单时出现问题 - 我得到NameError in Refinery::Brands::Admin::BrandsController#update
和uninitialized 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>
如果部分的其余部分有用,或者其他任何事情,请告诉我。任何帮助将不胜感激!