undefined method `current_user=' for #<SessionsController

  • Problem
  • Updated 4 years ago
  • In Progress
in 9.33 .. my installation doesn't seem to like

def test_sign_in(user)
controller.current_user = user
end

i get a

NoMethodError in 'SessionsController DELETE 'destroy' should sign a user out'
undefined method `current_user=' for #
/home/kirb/rails_projects/demo_app/spec/spec_helper.rb:32:in `test_sign_in'
/home/kirb/rails_projects/demo_app/spec/controllers/sessions_controller_spec.rb:70:

I can't seem to find anything on this...
Photo of kirbyK

kirby

  • 19 Posts
  • 0 Reply Likes
  • happy

Posted 5 years ago

  • 9
Photo of kirbyK

kirby

  • 19 Posts
  • 0 Reply Likes
i should add that the code runs fine. this issue is specific to my tests not passing :-p
Photo of gavinmcdermottG

gavinmcdermott

  • 4 Posts
  • 0 Reply Likes
Same issues here kirby - I'm new to this also. I'll keep digging and let you know if I find anything.
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Reply Likes
You might try using

self.current_user = user

The tests sometimes complain when you don't include the explicit self (which, in this context, is the controller).
Photo of kirbyK

kirby

  • 19 Posts
  • 0 Reply Likes
i replaced (in spec_helper.rb)

def test_sign_in(user)
controller.current_user = user
end

with

def test_sign_in(user)
self.current_user = user
end

i still get the following...
NoMethodError in 'SessionsController DELETE 'destroy' should sign a user out'
undefined method `current_user=' for #"

i do think this is the right path though.
Photo of gavinmcdermottG

gavinmcdermott

  • 4 Posts
  • 0 Reply Likes
Still the same issue here. Thanks for the feedback above, however.
Photo of Rails Tutorial

Rails Tutorial, Official Rep

  • 1009 Posts
  • 112 Reply Likes
Make sure you also have

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

gavinmcdermott

  • 4 Posts
  • 0 Reply Likes
The "self.current_user" wasn't working, instead used "@current_user" to get things to work properly.

Thanks for the replies - this is honestly a big help!
Photo of Ashish Dixit

Ashish Dixit

  • 5 Posts
  • 0 Reply Likes
I have a few questions regarding the current_user piece. First of all, do we not need to declare it as an attribute (class variable?) for SessionsController or rails automagically takes care of the details? The other question I had is in regards to the test for remember_me! feature where we call the following:

controller.current_user.should == @user

since we are calling controller.current_user.should, shouldn't we have current_user method defined to return the current user? The reason I am asking this is because I get:

undefined method `current_user' for #
../sample_app/spec/controllers/sessions_controller_spec.rb:51:

I am using the 2.3 version of the book.

Thanks!
Photo of Ian Simon

Ian Simon

  • 1 Post
  • 0 Reply Likes
I'm working through the 2.3 version and had the same problem. I changed spec_helper test_sign_in to @current_user - that just moved the problem down to the controller.should be_signed_in test. I noticed that in the v3.0 version of the tutorial, this test has been removed, so you can't be sure its correctly initialised before the destroy.
I've ended up replacing the use of 'test_sign_in', and copied the approach for signing in, with the 'before' construction of the factory user, then creating a user from session attributes:

Gavin - if you're following v3.0, you may want to add the
controller.should be_signed_in
test to check its initialised correctly, so you know your destroy test is valid
Photo of FranklinF

Franklin

  • 1 Post
  • 0 Reply Likes
I have this exact, precise issue. I've tried with/without Spork, same problem.
Photo of

  • 1 Post
  • 3 Reply Likes
The solution for me was over in this one: http://www.givesatisfaction.com/rails...

Both of the below methods should be in the session_helper.rb (I only had the last one, which forced me to make the change mentioned above with @current_user)

def current_user=(user)
@current_user = user
end

def current_user
@current_user ||= user_from_remember_token
end
Photo of

  • 2 Posts
  • 0 Reply Likes
Solved it for me. Specifically, adding

def current_user=(user)
@current_user = user
end

To sessions_helper.rb made the tests pass.
Photo of

  • 1 Post
  • 0 Reply Likes
Thanks. I had def current_user=(user) in the session controller, not the helper. Durrrrrrrr.
Photo of George Ulmer

George Ulmer

  • 3 Posts
  • 1 Reply Like
Worked for me!
Photo of George Ulmer

George Ulmer

  • 3 Posts
  • 1 Reply Like
I had the same problem! Thanks Tim!!!