我有以下两种模式:
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
我在Controller中尝试做的是获取所有项目的photoalbums列表:
class PhotoAlbumsController < ApplicationController
def index
@project = Project.find(params[:project_id])
@photoalbums = @project.photoalbums.all
end
end
但我收到以下错误?
uninitialized constant Project::Photoalbum
答案 0 :(得分:2)
将所有photoalbum
替换为photo_album
。
Rails很聪明,可以将photo_album
映射到PhotoAlbum
(请注意两个上限)。它仅在字符串的开头或下划线之后大写。如您所见,photoalbum
对应Photoalbum
(一个上限),在您的应用中不存在。
答案 1 :(得分:1)
尝试has_many :photo_albums
,然后@photo_albums = @project.photo_albums