0

I created a user and stored the id in a permanent cookie:

def save_user_id_cookie
  cookies.permanent.signed[:user_id] = @user_id
end

Here is a link.

and then try to access it:

helper_method :current_user

private
def current_user
  @current_user = @current_user || User.find(cookies.signed[:user_id])
end

Here is a link.

I see the cookie on my machine but when I try to load the homepage I get:

Couldn't find User without an ID

app/controllers/application_controller.rb:8:in `current_user'

The controller is here.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Alex
  • 690
  • 1
  • 9
  • 29

3 Answers3

1

Believe this line

@current_user = @current_user || User.find(cookies.signed[:user_id])

should be

@current_user = @current_user || User.find(cookies[:user_id])

*side note: for little less code you can try assigning like

@current_user ||= User.find(cookies[:user_id])
nowk
  • 32,822
  • 2
  • 35
  • 40
1

In your save_user_id_cookie:

def save_user_id_cookie
    cookies.permanent.signed[:user_id] = @user_id # may be @user.id?
    puts 'saved cookie'
end

@user_id is nil. I think you should use @user.id instead.

Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
0

Try this:

@current_user = @current_user || User.find(*cookies.signed[:user_id])

Notice the * before the cookies.

and yes, as @nash pointed out, that user_id should be actually user.id.
I didn't bother to look there for errors, as you said that you could see the cookie on your machine.

Jatin Ganhotra
  • 6,825
  • 6
  • 48
  • 71