2011-02-27 36 views
0

我有3個模型 - 成員,相冊和圖像。命名空間模型:未初始化的常量成員:: Album :: Image(NameError)

的member.rb文件是在/ app /模型目錄:

class Member < ActiveRecord::Base 

has_many :albums 

的album.rb文件是在/ app /模型/部件目錄:

class Member::Album < ActiveRecord::Base 

has_many :images 

圖像.RB文件在/ app /模型/件/專輯目錄:

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'文件夾正在加載,但我仍然得到這個錯誤。

回答

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

相關問題