0

こんにちは、私は現在、多くのユーザーが接続されたフォト アルバムを作成しようとしています。多対多 (1 人のユーザー + 複数のアルバム、1 つのアルバム + 複数のユーザー) になるように、それらの間の関係を修正しようとしています。ただし、モデルと移行に触れた後、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

終わり

ユーザーコントローラー

class 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

解決策: 私の UserAlbums モデルは、UserAlbum ではなく複数形でした。

4

1 に答える 1

0

UserAlbumモデルはいますか?

そうでない場合は、関連付けを有効にするために作成する必要があります。

class UserAlbum < ActiveReocrd::Base
  belongs_to :user
  belongs_to :album
end
于 2012-10-01T16:32:01.613 に答える