I’m trying to show a navigation bar if the user is signed in.
In my application.html.erb I have the line
<% if signed_in? %> – which is causing the error:
NoMethodError in Static_pages#home
undefined method '[]' for nil:NilClass
Extracted source (around line #16):
13: </head>
14: <body>
15:
16: <% if signed_in? %>
17: <div class="navbar navbar-fixed-top">
18: <div class="navbar-inner">
19: <div class="container">`
Here is my SessionsHelper:
def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
puts 'current_user called'
@current_user ||= User.find_by_remember_token(cookies.permanent[:remember_token])
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
My ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
end
I can’t seem to figure out why !current_user.nil? is breaking the page. When I comment out !current_user.nil? in the signed_in? method can get called without breaking the page. But I obviously don’t get the signed_in value I was hoping for.
exception was raised here:
cookies.permanent[:remember_token]so,
cookies.permanentis nil—- EDITED
got it,
use
cookies.permanent[:remember_token] =only for writing, but read it ascookies[:remember_token]