Rails错误未初始化常量User :: Friendship

时间:2014-11-19 20:09:53

标签: ruby-on-rails ruby ruby-on-rails-4 uninitialized-constant

我正在开发一个具有典型友谊模型的项目。用户类需要具有自引用关系,因此每个用户可以与多个朋友交朋友,并且可以与多个用户结识...简而言之,我需要完全相同的内容:

http://railscasts.com/episodes/163-self-referential-association?view=asciicast

但是在将整个视频写入信后我得到了这个错误

uninitialized constant User::Friendship

这就是我的用户模型:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships
end

友谊模特:

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name => "User"
end

和friendships_controller的创建方法

def create
    current_user = User.find(session[:user])
    # BELOW LINE HAS THE ERROR
    current_user.friendships.build(:friend_id => params[:friend_id])

    respond_to do |format|
      if @friendship.save
        format.html { redirect_to @friendship, notice: 'User Added To your team.' }
        format.json { render :show, status: :created, location: @friendship }
      else
        format.html { render :new }
        format.json { render json: @friendship.errors, status: :unprocessable_entity }
      end
    end
  end

我不知道这是否重要但我没有在视频中使用像Ryan这样的任何用户管理插件(我认为他使用OAuth或其他东西)...... 另外,我没有像在教程中那样通过nifty_scaffold生成友谊控制器 这是我用来生成脚手架的命令

rails generate scaffold Friendship user_id:integer friend_id:integer create destroy

加上我的ruby和rails版本,如果它有帮助......

Rails version 4.1.7
Ruby version 2.0.0p481

我一直试图调试我的代码,搜索互联网以弄清楚什么是错的,但注意似乎有效。关于可能出现什么问题的任何想法?

0 个答案:

没有答案