命名空间模型:未初始化的常量Member :: Album :: Image(NameError)

时间:2011-02-27 16:42:00

标签: ruby-on-rails namespaces models

我有3个模特 - 会员,专辑和图片。

member.rb文件位于/ app / models目录中:

class Member < ActiveRecord::Base

has_many :albums

album.rb文件位于/ app / models / member目录中:

class Member::Album < ActiveRecord::Base

has_many :images

image.rb文件位于/ app / models / member / album目录中:

class Member::Album::Image < ActiveRecord::Base

在我的routes.rb文件中,我有:

resources :members do

    resources :albums, :controller => 'members/albums' do

        resources :images, :controller => 'members/albums/images',:only => [:new, :create, :destroy] do
            get :edit, :on => :collection
            put :update, :on => :collection
        end
    end
end

但是当我尝试加载'/ members / 1 / albums'(和其他几个地方)时,我得到错误未初始化的常量Member :: Album :: Image。

我甚至尝试添加:

config.autoload_paths += %W(#{config.root}/app/models/member/album)

config.autoload_paths += Dir["#{config.root}/app/models/**/"]

到我的config / application.rb文件(并重新启动服务器),以确保我的所有文件嵌套在'app / models'文件夹中的子目录中,但我仍然收到该错误。

1 个答案:

答案 0 :(得分:0)

您正在做的事情实际上并不在控制器/模型中使用命名空间。这只是一个嵌套的路线。您迫使Rails在您的路由中使用命名空间控制器。而只是使用:

resources :members do
  resources :albums do
    resources :images, :only => [:new, :create, :destroy] do
      get :edit, :on => :collection
      put :update, :on => :collection
    end
  end
end

然后您根本不需要在控制器或模型中使用命名空间。

注意:建议您不要嵌套超过2深的路线。你现在在3,这会产生一些非常粗糙的网址,如http://example.com/members/42/albums/100/images/new