I have 3 models - Member, Album, and Image.
The member.rb file is in the /app/models directory:
class Member < ActiveRecord::Base
has_many :albums
The album.rb file is in the /app/models/member directory:
class Member::Album < ActiveRecord::Base
has_many :images
The image.rb file is in the /app/models/member/album directory:
class Member::Album::Image < ActiveRecord::Base
In my routes.rb file, I have:
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
But when I try to load '/members/1/albums' (and several other places), I get the error uninitialized constant Memb开发者_运维问答er::Album::Image.
I even tried adding:
config.autoload_paths += %W(#{config.root}/app/models/member/album)
and
config.autoload_paths += Dir["#{config.root}/app/models/**/"]
to my config/application.rb file (and restarted the server) to make sure that all my files nested in the subdirectories within the 'app/models' folder are being loaded, yet I still get that error.
What you're doing doesn't actually use namespaces in controllers/models. It's just a nested route. You're forcing Rails to use your namespaced controllers in your routes. Instead just use:
resources :members do
resources :albums do
resources :images, :only => [:new, :create, :destroy] do
get :edit, :on => :collection
put :update, :on => :collection
end
end
end
Then you don't need to bother with namespaces in your controllers or models at all.
Note: It's recommended that you don't nest routes more than 2-deep. You're currently at 3, which produces some pretty gnarly URLs like http://example.com/members/42/albums/100/images/new.
精彩评论