简体   繁体   English

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

[英]Rails 3 namespaced model and controller routing issue

I have namespaced model Equipment::Feature and namespaced controller in my admin part Admin::Equipment::FeaturesController . 我的管理部分Admin::Equipment::FeaturesController有命名空间模型Equipment::Feature和命名空间控制器。 Model is generic and is used as from within :admin namespace and for public website. 模型是通用的,可用于:admin名称空间和公共网站。 I've set up the routing for :admin and :equipment namespaces 我已经为:admin:equipment命名空间设置了路由

namespace :admin do
  namespace :equipment do
    resources :features
  end
end

Which gives me following routes: 这给了我以下路线:

 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

Pretty standard stuff. 很标准的东西。 But when I address /admin/equipment/features it throws uninitialized constant Admin::Equipment::FeaturesController::Equipment exception 但是当我处理/admin/equipment/features它会引发uninitialized constant Admin::Equipment::FeaturesController::Equipment异常

#index action in my Admin::Equipment::FeaturesController looks like 我的Admin::Equipment::FeaturesController #index操作看起来像

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

It did seem to work, until I declared Admin::Equipment namespace. 在我声明Admin::Equipment命名空间之前,它似乎确实起作用。 Before it was like Admin::EquipmentFeaturesController 之前像Admin::EquipmentFeaturesController

I guess this is some sort of namespace collision, but I don't get it - where does it come from? 我猜这是某种名称空间冲突,但我不明白-它来自哪里?

Thanks in advance! 提前致谢!

UPDATE Feature model (uses STI pattern) UPDATE 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 Fixing #index action as per answer below resolved the issue. UPDATE2按照下面的答案修复#index操作解决了该问题。 New code: 新代码:

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

I think it's now looking for Feature in Admin::Equipment , rather than in ::Equipment 我认为它现在正在Admin::Equipment寻找Feature ,而不是在::Equipment寻找Feature

Try specifying that there is no namespace, ie 尝试指定没有名称空间,即

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

Please create folder like this app/controllers/admin/equipment/features.rb 请创建像这样的文件夹app / controllers / admin / equipment / features.rb

And then edit your controller name to Admin::Equipment::FeaturesController 然后将您的控制器名称编辑为Admin :: Equipment :: FeaturesController

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

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM