2012-10-01 44 views
0

您好我目前正試圖建立一個相冊,附加了許多用戶,我試圖解決他們之間的關係,以便它是一個多對多(一個用戶+多個專輯,一個專輯+多個用戶)。但是,在我嘗試加載show.html.erb後,我在觸摸模型和遷移後收到錯誤。請幫忙!!Rails:未初始化的常量User :: UserAlbum?

錯誤:

uninitialized constant User::UserAlbum

show.html.erb

<% provide(:title, "Welcome, #{@user.name}!") %> 

<div> 
    You currently have <%= pluralize(@user.albums.count, "album") %> 
</div> 

<div> 
    <%= link_to "Create a new album!", new_user_album_path(@user) %> 
</div> 

<div> 
<% if @user.albums.any? %> 
hey 
<% else %> 
boo 
<% end %> 
</div> 

<%= link_to "Back", users_path %> 

相冊樣板

class Album < ActiveRecord::Base 
    attr_accessible :avatar, :name, :description 
    has_many :user_albums 
    has_many :users, :through => :user_albums 
    has_many :photos 
end 

用戶模型

class User < ActiveRecord::Base 

    has_secure_password 
    attr_accessible :email, :name, :password, :password_confirmation 
    validates_presence_of :password, :on => :create 

    validates_format_of :name, :with => /[A-Za-z]+/, :on => :create 
    validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create 
    validates_length_of :password, :minimum => 5, :on => :create 

    has_many :user_albums 
    has_many :albums, :through => :user_albums 
    accepts_nested_attributes_for :albums 

end 

照片模式(也許沒有必要看這個)

class Photo < ActiveRecord::Base 
    belongs_to :album 
end 

user_albums模型

class UserAlbums < ActiveRecord::Base 
belongs_to :album 
belongs_to :user 
end 

模式

ActiveRecord::Schema.define(:version => 20121001160745) do 

create_table "albums", :force => true do |t| 
    t.string "name" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.string "description" 
end 

create_table "photos", :force => true do |t| 
    t.string "avatar_file_name" 
    t.string "avatar_content_type" 
    t.integer "avatar_file_size" 
    t.datetime "avatar_updated_at" 
    t.datetime "created_at",   :null => false 
    t.datetime "updated_at",   :null => false 
    t.integer "album_id" 
end 

create_table "user_albums", :force => true do |t| 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    t.integer "user_id" 
    t.integer "album_id" 
end 

create_table "users", :force => true do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    t.string "password_digest" 
end 

用戶控制器

類UsersController < ApplicationController的

def index 
     @users = User.all 

     respond_to do |format| 
     format.html 
     format.json { render json: @users } 
     end 
    end 

    def new 
     @user = User.new 

     respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @user } 
     end 
    end 

    def create 
     @user = User.new(params[:user]) 

     respond_to do |format| 
     if @user.save 
      format.html { redirect_to @user, notice: 'User successfully created.' } 
      format.json { render json: @user, status: :created, location: @user } 
     else 
      format.html { render action: 'new' } 
      format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
     end 
    end 

    def show 
     @user = User.find(params[:id]) 
    end 

    def update 
     @user = User.update_attributes(params[:user]) 
     if @user.save 
     format.html { redirect_to @user, notice: 'User successfully updated.'} 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: 'edit' } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 

end 

SOLUTION: 我UserAlbums模型是複數而不是UserAlbum。

+0

你能說出發生錯誤的確切位置(哪一行)嗎? – mhutter

+0

嘿dratir。它說show.html.erb(@ user.albums.count)第4行,當我刪除相同的錯誤彈出爲該文件中的下一個@user引用 – Edmund

+0

我修好了!問題是我的模型被命名爲UserAlbums,而不是UserAlbum。 – Edmund

回答

0

您有UserAlbum型號嗎?

如果沒有 - 您需要爲您的關聯創建一個有效的關聯。

class UserAlbum < ActiveReocrd::Base 
    belongs_to :user 
    belongs_to :album 
end 
+0

你好,我現在要添加它 – Edmund

+0

可能是因爲我的模型是複數形式嗎? 'UserAlbums'? – Edmund

相關問題