Section 9.4.2: Undefined method 'admin?' for nil:NilClass

  • Question
  • Updated 3 years ago
  • Answered
Getting an error for "DELETE 'destroy' as a non-signed-in user should deny access" saying there's an undefined method 'admin?'. Is it because admin? is being accessed without a user being signed in at all? How can I prevent this? I may have overlooked some code somewhere...
Photo of derek.brameyer

derek.brameyer

  • 3 Posts
  • 0 Reply Likes
  • sad

Posted 5 years ago

  • 7
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Reply Likes
This could be because the tests don't know about the new admin column. You might have to run

rake db:test:prepare

to get it to work.
Photo of liamgmas

liamgmas

  • 2 Posts
  • 0 Reply Likes
This worked for me. Any btw, I was receiving this autospec "undefined method 'admin?'" error trucking between 10.3 & 10.4. Great job Michael.
Photo of Psychodad

Psychodad

  • 2 Posts
  • 0 Reply Likes
this didn't help. the test still fails.


describe "as a non-signed-in user" do
it "should deny access" do
delete :destroy, :id => @user

undefined method `admin?' for nil:NilClass

is it a problem with the class of a non signed-in user?
Photo of triptec

triptec

  • 4 Posts
  • 12 Reply Likes
I got this problem aswell, could it have something to do with when a user isn't signed in current_user isn't set?
Photo of triptec

triptec

  • 4 Posts
  • 12 Reply Likes
I had missed the :destroy in the before_filter :authenticate in app/controllers/users_controller.rb
before_filter :authenticate, :only => [:index, :edit, :update, :destroy]
Photo of blackhuey

blackhuey

  • 2 Posts
  • 0 Reply Likes
Same here, thanks!
Photo of bmckelvey

bmckelvey

  • 1 Post
  • 0 Reply Likes
Ditto. The issue, for anyone wondering, is that if you don't have

before_filter :authenticate, :only => [ ... :destroy]

what gets applied is

before_filter :admin_user

admin_user checks the admin? method (which ruby implies from the admin attribute). Since the user is not signed in, the user object is nil, and nil doesn't have an admin? method, hence your error.
Photo of Psychodad

Psychodad

  • 2 Posts
  • 0 Reply Likes
yeah. that did solve it for me.
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Reply Likes
Glad you figured it out!
Photo of robgough

robgough

  • 1 Post
  • 1 Reply Like
I had the same issue, and had missed adding the :destroy to the before_filter:authenticate line.

Thanks! It's a great rails tutorial.
Photo of Tsega

Tsega

  • 6 Posts
  • 0 Reply Likes
I missed it, too. Thanks for being the first silly person to discover that :-)
Photo of shajinvk

shajinvk

  • 1 Post
  • 0 Reply Likes
still am getting d same error..
Photo of Doug Ireton

Doug Ireton

  • 1 Post
  • 0 Reply Likes
In listing 10.4.1, I also missed adding the :destroy action to the :authenticate before_filter:

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

As soon as I added the :destroy action to the before_filter above, my tests passed.
Photo of MarinaMartin

MarinaMartin

  • 5 Posts
  • 0 Reply Likes
Should someone else get this error in section 10.4 ... I deviated from the tutorial and left :index viewable to anyone, not just signed-in users. I had to wrap my code with to make the index page display publicly.
Photo of JVenator

JVenator

  • 7 Posts
  • 0 Reply Likes
I too had made the mistake of not including ':destroy' in my 'before_filter :authenticate' hash. However, all my tests were already passing (not sure how).

I only noticed the error because when I try using the app online (from the heroku deployment), I get an error when I click the 'Users' link. According to he command line heroku log it tells me that I an (undefined method 'admin?').

I fixed the before_filter referenced above and pushed to heroku, but it still doesn't work. Any suggestions? I'm probably not providing enough or the right info here. Help appreciated...
Photo of MarinaMartin

MarinaMartin

  • 5 Posts
  • 0 Reply Likes
Did you update Heroku's database?

heroku rake db:migrate
Photo of JVenator

JVenator

  • 7 Posts
  • 0 Reply Likes
I realize I did not, which means the added column for 'admin' with a default of "false" did not make it over.

However, I attempted to run the ($ heroku rake db:migrate) command but received an error message "rake aborted! Permission denied..."

Via the ($ heroku keys) command I validated that I have two ssh-rsa keys on my machine. Both are linked to the same email address (perhaps this is an issue?). I'm 99% sure I'm on the same machine I started the tutorial on, but I'm sure it shouldn't matter if I have the correct ssh keys.

Any thoughts? And thanks for the fast initial response!
Photo of MarinaMartin

MarinaMartin

  • 5 Posts
  • 0 Reply Likes
Sorry, not sure. Perhaps go back through the Heroku connection steps from the beginning and see if that fixes the authentication issue?
Photo of Syed Raza Khalid

Syed Raza Khalid

  • 1 Post
  • 0 Reply Likes
I was having same issue was blaming Tutorial author My appology..
Photo of brentbuchholz

brentbuchholz

  • 1 Post
  • 0 Reply Likes
Same issue, I forgot to put :destroy into the :authenticate before_filter, now my tests pass.

Thanks for sharing! Would have taken me forever to figure out on my own (I'm still very new to rails and ruby in general).
Photo of seto

seto

  • 1 Post
  • 0 Reply Likes
ya problem:
Photo of Zach Phillips

Zach Phillips

  • 1 Post
  • 0 Reply Likes
Not sure if it's worth mentioning this, but it also matters that the before filters are in the right order, with admin_user coming last. I got hung up on this for a while as I wasn't following along exactly with the tutorial, trying to do my own thing using Michael's methods.

Had them in the wrong order, and so I almost killed myself before figuring that out.

I'm not too bright.