Rails 3命名空间模型和控制器路由问题

时间:2012-06-16 14:58:07

标签: ruby-on-rails ruby-on-rails-3

我的管理部分Equipment::Feature中有命名空间模型Admin::Equipment::FeaturesController和命名空间控制器。模型是通用的,用于:admin命名空间内和公共网站。我已为:admin:equipment命名空间

设置了路由
namespace :admin do
  namespace :equipment do
    resources :features
  end
end

这给了我以下路线:

 admin_equipment_features     GET /admin/equipment/features(.:format)          admin/equipment/features#index
                              POST /admin/equipment/features(.:format)         admin/equipment/features#create
 new_admin_equipment_feature  GET /admin/equipment/features/new(.:format)      admin/equipment/features#new
 edit_admin_equipment_feature GET /admin/equipment/features/:id/edit(.:format) admin/equipment/features#edit
 admin_equipment_feature      GET /admin/equipment/features/:id(.:format)      admin/equipment/features#show
                              PUT /admin/equipment/features/:id(.:format)      admin/equipment/features#update
                              DELETE /admin/equipment/features/:id(.:format)   admin/equipment/features#destroy

非常标准的东西。但是当我解决/admin/equipment/features时会抛出uninitialized constant Admin::Equipment::FeaturesController::Equipment异常

我的#index中的

Admin::Equipment::FeaturesController操作看起来像

def index
  @features = Equipment::Feature.all
end

在我声明Admin::Equipment命名空间之前,似乎确实有效。之前就像Admin::EquipmentFeaturesController

我猜这是某种命名空间冲突,但我不明白 - 它来自哪里?

提前致谢!

更新 Feature模型(使用STI模式)

class Equipment::Feature < ActiveRecord::Base

  attr_accessible :category_id, :name_en, :name_ru, :type

  belongs_to :category, :class_name => 'Equipment::Category'

  has_many :item_features, :class_name => 'Equipment::ItemFeature'
  has_many :items, :through => :item_features

  translates :name
end

class FeatureBoolean < Equipment::Feature

end

class FeatureNumeric < Equipment::Feature

end

class FeatureString < Equipment::Feature

end

class FeatureRange < Equipment::Feature

end

UPDATE2 根据以下答案修复#index操作解决了问题。新代码:

def index
  @features = ::Equipment::Feature.all
end

2 个答案:

答案 0 :(得分:2)

我认为现在正在Feature中寻找Admin::Equipment,而不是::Equipment

尝试指定没有名称空间,即

def index
  @features = ::Equipment::Feature.all
end

答案 1 :(得分:1)

请创建此app / controllers / admin / equipment / features.rb

这样的文件夹

然后将您的控制器名称编辑为Admin :: Equipment :: FeaturesController

class Admin::Equipment::FeaturesController < ActiveRecord::Base

end