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.brameyerD

derek.brameyer

  • 3 Posts
  • 0 Likes
  • sad

Posted 5 years ago

  • 7
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 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 liamgmasL

liamgmas

  • 2 Posts
  • 0 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 PsychodadP

Psychodad

  • 2 Posts
  • 0 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 triptecT

triptec

  • 4 Posts
  • 12 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 triptecT

triptec

  • 4 Posts
  • 12 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 blackhueyB

blackhuey

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

bmckelvey

  • 1 Post
  • 0 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 PsychodadP

Psychodad

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

Rails Tutorial, Official Rep

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

robgough

  • 1 Post
  • 1 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 Likes
I missed it, too. Thanks for being the first silly person to discover that :-)
Photo of shajinvkS

shajinvk

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

Doug Ireton

  • 1 Post
  • 0 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 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 JVenatorJ

JVenator

  • 7 Posts
  • 0 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 Likes
Did you update Heroku's database?

heroku rake db:migrate
Photo of JVenatorJ

JVenator

  • 7 Posts
  • 0 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 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 KhalidSR

Syed Raza Khalid

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

brentbuchholz

  • 1 Post
  • 0 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 setoS

seto

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

Zach Phillips

  • 1 Post
  • 0 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.