Rails:未初始化的常量(NameError)

时间:2014-06-17 23:55:35

标签: ruby-on-rails-4 railstutorial.org

我陷入了Rails教程的第11章(“第11.2.5节使用Ajax的工作跟随按钮”)。我正在

relationships_controller_spec.rb:3:in `<top (required)>': uninitialized constant RelationshipsController (NameError)

这是我的控制器规范(/sample_app/spec/controllers/relationships_controller_spec.rb)错误抱怨第3行:

require 'spec_helper'

describe RelationshipsController do
let(:user) {FactoryGirl.create(:user)}
let(:other_user) {FactoryGirl.create(:user)}

before {sign_in user, no_capybara: true}

describe "create a relationship with Ajax" do

it "should increment the Relationship count" do
  expect do
    xhr :post, :create, relationship: { followed_id: other_user.id}
  end.to change(Relationship, :count).by(1)
end

it "should respond with success" do
  xhr :post, :create, relationship: {followed_id: other_user.id}
  expect(response).to be_success
end
end

describe "destroying a relationship with Ajax" do

before {user.follow!(other_user)}

let(:relationship) do
  user.relationships.find_by(followed_id: other_user.id)
end

it "should decrement the Relationship count" do
  expect do
    xhr :delete, :destroy, id: relationship.id
  end.to change(relationship, :count).by(-1)
end

it "should respond with success" do
  xhr :delete, :destroy, id: relationship.id
  expect(response).to be_success
end
end
end

这是控制器(/sample_app/app/controllers/relatonships_controller.rb)本身:

class RelationshipsController < ApplicationController
before_action :signed_in_user

def create
@user = User.find(params[:relationship][:followed_id])
current_user.follow!(@user)
#redirect_to @user replaced with the code below:
respond_to do |format|
  format.html {redirect_to @user}
  format.js
end
end

def destroy
@user = Relationship.find(params[:id]).followed
current_user.unfollow!(@user)
#redirect_to @user replaced with the code below:
respond_to do |format|
  format.html {redirect_to @user}
  format.js
end
end
end

我做错了什么?

2 个答案:

答案 0 :(得分:2)

文件名中有拼写错误:/ relations,中继和onships之间没有。

答案 1 :(得分:0)

你的文件名是什么?在Rails中,这很重要。确保您的控制器实际上被称为relationships_controller.rb

通常可以将文件名或类称为单数形式,另一种称为复数形式,但似乎缺少'r'。就像你的文件名是relationships_controlle.rb不知怎的。