开发者

Modeling Question

开发者 https://www.devze.com 2023-01-21 11:49 出处:网络
I have the following two models: class Project < ActiveRecord::Base has_many :photoalbums end class PhotoAlbum < ActiveRecord::Base

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

精彩评论

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