Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3986062
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:58:00+00:00 2026-05-20T05:58:00+00:00

I have a Rails 3 project in which I want to store the current

  • 0

I have a Rails 3 project in which I want to store the current company selected in a session variable.

I’m working with the staff controller spec and would like to stub out current_company for now as I’m isolating my spec example for the staff new controller action.

it "should call current_company" do 
  company = mock_model(Company, :id => "1")
  controller.should_receive(:current_company).and_return(company)
  get :new
end

Here is my new action for the staff controller

  def new
    @staff = Staff.new
    @staff.company_id = current_company.id
  end

I keep getting error

Failure/Error: get :new
     NameError:
       undefined local variable or method `current_company' for #<StaffsController:0x000000028d6ad8>

I’ve also tried just stubbing it out instead of using should_receive

  controller.stub!(:current_company).and_return(company)

I get the same error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:58:00+00:00Added an answer on May 20, 2026 at 5:58 am

    I think it was bombing out on the ‘should be successful’ example/test, so I’ve put my stubbing in a before block.

    require 'spec_helper'
    
    describe StaffsController do
    
      describe "GET 'new'" do
        let(:staff) { mock_model(Staff, :company_id= => nil)}
        let(:company) { mock_model(Company, :id => 1)}
    
        before do
          Staff.stub!(:new).and_return(staff)
          controller.stub!(:current_company).and_return(company)
        end
    
        it "should be successful" do
          get :new
          response.should be_success
        end
    
        it "should call current_company" do 
          controller.should_receive(:current_company).and_return(company)
          get :new
        end
      end
    end
    

    This works for:

    class StaffsController < ApplicationController
      def new
        @staff = Staff.new
        current_company.id
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on rails project, which had been made using noSql(Mongoid). Every
I have created a instance variable in rails project, which gets its value from
I have a Rails 2.2 project in which I want to override the functionality
I have a legacy project using Rails 2.3.5 but I can't find with which
I have a rails 3.2 project. There are a bid object and i want
Starting a new rails project and we have a well-thought-out color palette, and want
So I'm working on a new rails project which involves running multiple website from
I'm working on a Rails project where I want to allow users to create
My current project is in rails 2.3.5 which uses restful authentication. I am moving
I have a project which does not follow the rails nameing conventions, because it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.