【问题标题】:Rails 3 namespaced model and controller routing issueRails 3 命名空间模型和控制器路由问题
【发布时间】:2012-06-16 14:58:06
【问题描述】:

我在管理部分 Admin::Equipment::FeaturesController 中有命名空间模型 Equipment::Feature 和命名空间控制器。模型是通用的,用于: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

更新2 根据下面的答案修复 #index 操作解决了该问题。新代码:

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

【问题讨论】:

  • 请出示您的特征模型中的代码。
  • 更新问题。 Feature 模型使用 STI 模式并具有 type 列,其中 STI 类名称符合 Rails 约定

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


【解决方案1】:

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

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

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

【讨论】:

  • 哦,谢谢!有效!我不知道带有 :: 类型的“重置”命名空间解析的前缀命名空间。
【解决方案2】:

请创建这样的文件夹 app/controllers/admin/equipment/features.rb

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

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

end

【讨论】:

    猜你喜欢
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-09
    相关资源
    最近更新 更多