开发者

Modeling Question

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜