user_signed_in?总是返回false [英] user_signed_in? always returns false

查看:119
本文介绍了user_signed_in?总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在追踪这些轨道。

I have been following these railscasts

http://railscasts.com/episodes/209-introducing-devise &&
http://railscasts.com/episodes/233-engage-with-devise
用于用户身份验证,但是每当我尝试查看用户是否登录时,响应始终为false。

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.

我尝试了一堆的事情来解决这个问题。我尝试过不同版本的devise和devise_rpx_connectable。我尝试在配置文件中更改设置。我尝试运行rails生成设计:安装一段时间与不同的宝石版本。

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.

奇怪的是,当我将RPXNow.api_key变成一些假的,我没有收到错误,这是意想不到的行为。

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.

这是我的application.html.erb文件中的代码:

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) %>

以下是我的用户模型user.rb文件中的代码

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'

devise_rpx_connectable gem版本0.2.2

and this gets the devise_rpx_connectable gem version 0.2.2

这是我的初始化器/ devise.rb文件中的内容:

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

这是在我们的routes.rb文件中:

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

ply的评论---
我做了rails g migration add_rpx_to_users rpx_identifier:string

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

并运行rake db:migrate on:

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.

非常感谢!

推荐答案

这可能是因为您可能已经在应用程序控制器中使用current_user方法。因此,设计不会看到它的current_user方法的实现,因此它的帮助方法总是返回false。只需删除或重命名您正在使用的current_user方法,并解决问题。

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.

这篇关于user_signed_in?总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆