I have the following two models:
class Project < ActiveRecord::Base
has_many :photoalbums
end
class PhotoAlbum < ActiveRecord::Base
belongs_to :开发者_运维问答space
end
routes.rb:
resources :projects do
resources :photo_albums
end
What I'm trying to do in the Controller is get a list of all the project's photoalbums:
class PhotoAlbumsController < ApplicationController
def index
@project = Project.find(params[:project_id])
@photoalbums = @project.photoalbums.all
end
end
But I'm getting the following error?
uninitialized constant Project::Photoalbum
Replace all occurrences of photoalbum
with photo_album
.
Rails is smart and maps your photo_album
to PhotoAlbum
(note the two caps). It only capitalises at the beginning of a string or after an underscore. As you've seen, photoalbum
corresponds to Photoalbum
(one cap), which does not exist in your application.
Try has_many :photo_albums
, and then @photo_albums = @project.photo_albums
精彩评论