Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 Devise Session Has value but <% if user_signed_in? %> is false

I hope you can help, have been struggling with this one for a while now :| Im using devise and Rails 3

When I click on Sign In, I get the devise Sign In page, I punch in the username and pw and click login.

After I clicked login I get redirected to the main page but now I still see the Sign In link, wich means that <% if user_signed_in? %> is still false. But it seems that there is some values in the session variable after I logged in, what is going on here? I have been using devise for some time and havent had any problems before. Thank guys!


Before I logged in with devise
<%= session %>
no value

<% if user_signed_in? %>
**is false**

After I logged in with devise

<%= session %>
_csrf_tokenAMUwVLu6G6rWfKICB43PYApFsYFRjVyJDSc2oU88uEk=warden.user.user.keyUser342$2a$10$.zslfggeUqvq.m/5LNSolOsession_id0db80c26bc36a4c1c74c223655dcb092

<% if user_signed_in? %>
**is false**

EDIT:

my routes.rb file

Cybercellar3::Application.routes.draw do
  devise_for :users

  get "home/index"


<% if signed_in? %>
**is still false**

EDIT2:

user.rb

class User < ActiveRecord::Base
      # Include default devise modules. Others available are:
      # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      # Setup accessible (or protected) attributes for your model
      attr_accessible :email, :password, :password_confirmation, :remember_me
    end

I think I found my problem in one of my controllers

application_controller.rb

    #facebook stuff
    def current_user
        @current_user ||= User.find_by_id(session[:user_id])
    end

    def signed_in?
        !!current_user
    end

    helper_method :current_user, :signed_in?

    def current_user=(user)
        @current_user = user
        session[:user_id] = user.id
    end
    #facebook stuff

after I removed this block it seemed to work perfectly :)

like image 802
Francois Avatar asked Oct 23 '22 09:10

Francois


1 Answers

Devise has a concept of "scopes" so that you can sign in multiple types of users simultaneously, e.g. an admin user and a regular user. To check if a user of any scope is signed in, use signed_in?. The scopes are defined by the naming of the model class and the routes.rb file. Be sure they match. Can you post both? To test this out you can also try an integration test.

like image 135
Wolfram Arnold Avatar answered Oct 26 '22 23:10

Wolfram Arnold