【问题标题】:Rails: uninitialized constant User::UserAlbum?Rails:未初始化的常量 User::UserAlbum?
【发布时间】:2012-10-01 16:28:57
【问题描述】:

您好,我目前正在尝试建立一个附有许多用户的相册,并且我正在尝试修复它们之间的关系,以便它是多对多的(一个用户 + 多个相册,一个相册 + 多个用户)。但是,我在触摸模型和迁移后尝试加载 show.html.erb 时收到错误消息。请帮忙!!

错误:

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

结束

用户控制器

类用户控制器

    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

解决方案: 我的 UserAlbums 模型是复数而不是 UserAlbum。

【问题讨论】:

  • 你能说出错误发生在哪里(哪一行)?
  • 嘿dratir。它说 show.html.erb (@user.albums.count) 的第 4 行,当我删除该文件中的下一个 @user 引用时会弹出相同的错误
  • 我修好了!!!问题是我的模型被命名为“s”而不是 UserAlbum。

标签: ruby-on-rails


【解决方案1】:

你有UserAlbum 模型吗?

如果没有 - 您需要创建一个关联才能生效。

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

【讨论】:

  • 会不会是因为我的模型是复数形式? UserAlbums?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-15
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
相关资源
最近更新 更多