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 3956036
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:18:47+00:00 2026-05-20T02:18:47+00:00

I’ve been working through Michael Hartl’s Rails tutorial (which is unbelievably awesome by the

  • 0

I’ve been working through Michael Hartl’s Rails tutorial (which is unbelievably awesome by the way).

Anyway, everything has been going pretty well and I’ve nearly reached the end of chapter 10. The problem is that my rspec tests have started to generate some failures and I can’t figure out what’s wrong.

The first failure occurred when I was working through the section on destroying users. The test

before :each do
  @user = Factory :user
end

describe "as a non-signed-in user" do
  it "should deny access" do
    delete :destroy, :id => @user
    response.should redirect_to(signin_path)
  end
end

gives the error:

UsersController DELETE 'destroy' as a non-signed-in user should deny access
 Failure/Error: delete :destroy, :id => @user
 NoMethodError:
   undefined method `admin?' for nil:NilClass
 # ./app/controllers/users_controller.rb:76:in `admin_user'
 # ./spec/controllers/users_controller_spec.rb:308:in `block (4 levels) in <top (required)>'

Here’s the code the message references in users_controller:

def admin_user
  # the error tels me that current_user = NilClass
  redirect_to(root_path) unless current_user.admin?
end

So I guess this would suggest that current_user isn’t working correctly and is being set to nil. Now current_user involves a lot of methods of the SessionsHelper which (afaik) deal with setting the users ID in secure cookies and referencing the cookie as they move around the site. So this suggests that there is something wrong with the cookies.

I’ve checked the browser and the cookie is being set, I’ve also gone over every part of the code and it all replicates the tutorial exactly as far as I can tell.

Is there something else I should be looking at?

Appendix

Here is the contents of the SessionsHelper module:

module SessionsHelper

    def sign_in user
        # rails represents cookies as a hash and deals with the conversion for us
        # making the cookie "signed" makes it impervious to attack
        cookies.permanent.signed[:remember_token] = [user.id, user.salt]
        # this line calls the assignment operator below
        self.current_user = user
      end

      def current_user=(user)
        @current_user = user
      end

      # this is a getter method
      def current_user
        # this sets @current_user = to the user corresponding to the remember token
        # but only if @current user is undefined. ie it only works once
        @current_user ||= user_from_remember_token
      end

    def signed_in?
        # return true if current_user is not nil
        !current_user.nil?
      end

      def sign_out
        cookies.delete(:remember_token)
        self.current_user = nil
      end

      def current_user? user
        # returns true if the user object == the current_user object
        user == current_user
      end

      def authenticate
        deny_access unless signed_in?
      end

    def deny_access
        store_location
        # this is a shortcut for flash notices: flash[:notice] = "Please sign in to access this page."
        redirect_to signin_path, :notice => "Please sign in to access this page."
      end

      def redirect_back_or(default)
        redirect_to(session[:return_to] || default)
        clear_return_to
      end

  private

    def user_from_remember_token
      # the * allows us to give a 2 element array to a method expecting 2 seperate arguments
      User.authenticate_with_salt(*remember_token)
    end

    def remember_token
      # return [nil, nil] if the :remember_token cookie is nil
      cookies.signed[:remember_token] || [nil, nil]
    end

    def store_location
      # stores the url the browser was requesting
      session[:return_to] = request.fullpath
    end

    def clear_return_to
      session[:return_to] = nil
    end
  end
  • 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-20T02:18:48+00:00Added an answer on May 20, 2026 at 2:18 am

    In your spec ypu are trying to delete user, while you are not logged in, so current_user is nil. You should prevent access to this action non singed in user.

    class UsersController < ApplicationController
      before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
      ....
    

    Just as it is described in example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
We're building an app, our first using Rails 3, and we're having to build
I am trying to loop through a bunch of documents I have to put
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.