设计 - 无法在 Rails 视图中显示登录或注销 [英] devise - can't display sign in or sign out in rails view

查看:30
本文介绍了设计 - 无法在 Rails 视图中显示登录或注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 devise 进行基本身份验证.当我转到 localhost:3000/users/sign_in 时,我将能够登录,或者如果我在登录时去那里,我会收到相应的消息您已经登录."

但是,user_signed_in? 总是评估为 false,即使在成功登录后也是如此

<% if user_signed_in?%>欢迎 <%= current_user.email %>!<%= link_to "退出", destroy_user_session_path, :method =>:删除%><%其他%><%= link_to "连接 Facebook", "/auth/facebook" %><%= link_to "退出", destroy_user_session_path, :method =>:删除%><%结束%>

<% flash.each do |name, msg|%><%= content_tag :div, msg, :id =>"flash_#{name}" %><%结束%>

这里是config/application.rb

class ApplicationController <动作控制器::基础保护_从_伪造与::异常before_filter :authenticate_user!私人的定义当前用户@current_user ||= User.find(session[:user_id]) 如果 session[:user_id]结尾helper_method :current_user结尾

我怎样才能让 user_signed_in? 成为真的?current_user 也一直是false,不管我是否登录成功

解决方案

这可能是因为您正在定义自己的 current_user 方法.这将覆盖 Devise 提供的那个.

如果您查看 user_signed_in 的来源?方法,可以看到当前用户(Devise定义的用户)被该方法使用.Devise 很可能不会将当前用户保存到 session[:user_id],这就是您的方法正在检查的内容.因此,当 Devise 从 user_signed_in? 调用您的方法时,它总是返回 false.

尝试从 ApplicationController 中删除当前用户方法,看看是否能解决问题.

I'm using devise to do basic authentication for now. When I go to localhost:3000/users/sign_in I will be able to sign in or if I go there when I'm logged in I'll get the appropriate message "You are already signed in."

However, user_signed_in? is always evaluating to false, even after successful sign in

<div id="user_nav">
    <% if user_signed_in? %>
      Welcome <%= current_user.email %>!
      <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
    <% else %>
      <%= link_to "Connect With Facebook", "/auth/facebook" %>
      <%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
    <% end %>
  </div>
  <% flash.each do |name, msg| %>
    <%= content_tag :div, msg, :id => "flash_#{name}" %>
  <% end %>
</div>

Here is config/application.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :authenticate_user!
  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end

How can I get user_signed_in? to be true? current_user also continues to be false, whether I successfully sign in or not

解决方案

This is probably because you are defining your own current_user method. This will then override the one that is provided by Devise.

If you look the source for the user_signed_in? method, you can see that current user (the one defined by Devise) is used by this method. It is likely that Devise doesn't save the current user to session[:user_id], which is what your method is checking. Consequently, when Devise calls your method from within user_signed_in?, it always returns false.

Try removing the current user method from your ApplicationController and see if that resolves the problem.

这篇关于设计 - 无法在 Rails 视图中显示登录或注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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