简体   繁体   中英

user_signed_in? always returns false

I have been following these railscasts

http://railscasts.com/episodes/209-introducing-devise && http://railscasts.com/episodes/233-engage-with-devise for user authentication, however whenever I try to see if a user is signed in, the response is always false.

I tried a bunch of things to try remediating this issue. I tried different versions of devise and devise_rpx_connectable. I tried changing settings around in the configuration file. I tried running 'rails generate devise:install' a bunch of times with different gem versions.

The weird thing is that when I change around the RPXNow.api_key to something bogus, I don't get an error, which is unexpected behavior.

Here is the code in my application.html.erb file:

<% if user_signed_in? %>
    Signed in as <%= current_user.email %>. Not you?
    <%= link_to "Sign out", destroy_user_session_path %>
<% else %>  
    <%= link_to_rpx "Sign in", user_session_url %>
<% end %>
<%= javascript_include_rpx(root_url) %>

Here is the code in my user model user.rb file

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :rpx_connectable

    attr_accessible :email, :password, :password_confirmation

    validates :email, :uniqueness => true
    .....other stuff....
end

Here is part of my Gemfile:

gem 'rails', '3.2.6'

gem 'devise','2.0.0'
gem 'devise_rpx_connectable'

and this gets the devise_rpx_connectable gem version 0.2.2

Here is whats in my initializers/devise.rb file:

Devise.setup do |config|
    config.mailer_sender = "please-change-me-at-config-initializers-devise@example.com"
    config.apply_schema = false
    require 'devise/orm/active_record'
    config.case_insensitive_keys = [ :email ]
    config.strip_whitespace_keys = [ :email ]
    config.skip_session_storage = [:http_auth]
    config.stretches = Rails.env.test? ? 1 : 10
    config.reconfirmable = true
    config.use_salt_as_remember_token = true
    config.reset_password_within = 6.hours
    config.sign_out_via = :delete
    config.rpx_application_name = "appname"
    RPXNow.api_key = "<any_key_we_want_apparently>"
end

And this is in our routes.rb file:

AppName::Application.routes.draw do
    root :to => "pages#home"
    match 'users/:id' => "users#index" 
    devise_for :users
end

--editted after ply's comment--- I did rails g migration add_rpx_to_users rpx_identifier:string

and ran rake db:migrate on this:

class AddRpxToUsers < ActiveRecord::Migration
    def change
        add_column :users, :rpx_identifier, :string
    end
end

If anyone has any thoughts, please share. I have been struggling with this issue all day.

Thank you very much!

This might be because you might already be using the current_user method in the application controller. Due to that, the devise will not see its implementation of current_user method and hence it always returns false for its helper methods. Just remove or rename the current_user method which you are using and this solves the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM