Nick
Nick

Reputation: 3124

Rails: uninitialized constant (NameError)

I am stuck in Chapter 11 of the Rails tutorial ("Section 11.2.5 A working follow button with Ajax"). I am getting

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

Here is my controller spec (/sample_app/spec/controllers/relationships_controller_spec.rb) The error complains about line 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

Here is the controller (/sample_app/app/controllers/relatonships_controller.rb) itself:

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

What am I doing wrong?

Upvotes: 0

Views: 4777

Answers (2)

frandroid
frandroid

Reputation: 1416

You have a typo in the filename: /relatonships, no i between relat and onships.

Upvotes: 2

bratsche
bratsche

Reputation: 2674

What are your filenames? In Rails this can be important. Make sure your controller is actually called relationships_controller.rb.

It's common to maybe call either the filename or the class the singular form and the other the plural form, but it seems like something is missing an 'r'. Like your filename is relationships_controlle.rb somehow.

Upvotes: 0

Related Questions