3

I have this message in the browser when I open it. I'm using Rails 5.1.1, ruby 2.4.1, puma 3.9.1.

"undefined method `signed' for nil:NilClass" referring to cookies.signed

I have the same project in Rails 5.0.3 and it's working. I've commented the line, but it crashes in every line with "cookies". I tried to start a new Rails project but the problem persists. Is this an issue of Rails 5.1.1?

My sessions_helper.rb file has this code:

# frozen_string_literal: true

# Helper for Sessions
module SessionsHelper
  # Logs in the given user.
  def log_in(user)
    session[:user_id] = user.id
  end

  # Remembers a user in a persistent session.
  def remember(user)
    user.remember
    cookies.permanent.signed[:user_id] = user.id
    cookies.permanent[:remember_token] = user.remember_token
  end

  # Returns the user corresponding to the remember token cookie.
  def current_user
    if (user_id = session[:user_id])
      @current_user ||= User.find_by(id: user_id)
    elsif (user_id = cookies.signed[:user_id]) # Error in this line
      user = User.find_by(id: user_id)
      if user && user.authenticated?(cookies[:remember_token])
        log_in user
        @current_user = user
      end
    end
  end

  # Returns true if the user is logged in, false otherwise.
  def logged_in?
    !current_user.nil?
  end

  # Forgets a persistent session.
  def forget(user)
    user.forget
    cookies.delete(:user_id)
    cookies.delete(:remember_token)
  end

  # Logs out the current user.
  def log_out
    forget(current_user)
    session.delete(:user_id)
    @current_user = nil
  end
end
  • Where is this module used? If it's not inside a controller you're going to have problems. – tadman Jun 15 '17 at 02:20
  • https://www.railstutorial.org/book/advanced_login I'm following this tutorial, the module is called from a sessions_controller, and in the application controller I've included this line include SessionsHelper – Jhonatan Tovar Jun 15 '17 at 02:31
  • It's really odd to see `cookies` as `nil` inside the context of a controller. It should auto-instantiate. – tadman Jun 15 '17 at 14:20
  • 1
    I was trying everything and found that the cookies aren't available at the root path and all my static pages. I can access the cookie value in the login and users paths, but I don't know why are the presenting this behavior. I need to access the cookie value in the root path so the user is logged in automatically. – Jhonatan Tovar Jun 15 '17 at 22:33
  • You should probably inspect the headers of the requests in your browser (Developer Mode) and see how the cookies are being set. – tadman Jun 16 '17 at 02:42
  • I have the same PROBLEM! After i set my cookie the static pages can't seem to find it. What ever i do the cookie is nil. But on pages with content the cookie is set. Did you ever figure out what to do? – Peter Højlund Palluth Dec 20 '17 at 12:46
  • 1
    @PeterAndersen, do you have a static page called cookies? – Jhonatan Tovar Jan 01 '18 at 18:12

0 Answers0