【问题标题】:undefined method `user_signed_in?' - Devise未定义的方法“user_signed_in?” - 设计
【发布时间】:2011-05-19 05:26:12
【问题描述】:

已解决:我在 ApplicationController 中使用了“clear_helpers”方法,因此它阻止了 Devise 加载他的助手。

你好,

当我第一次在“开发”模式下运行我的应用程序时,我得到了这个错误,当我重新加载错误消失时。当我在“生产”上运行时,错误仍然存​​在,因为生产只加载应用程序一次(?),所以发生了什么?有人可以帮忙吗?

错误:

undefined method `user_signed_in?' for #<#<Class:0x10312ddc0>:0x103127100>

Extracted source (around line #16):

13: 
14:             #top
15:                 = link_to '', root_path, :id => 'logo'
16:                 - if user_signed_in?
17:                     #session
18:                         %p= raw "Olá <b>#{current_user.email}</b>"          
19:                         #myaccount.button{:onclick => "javascript: document.location.href = '#{edit_user_registration_path}'"}

我正在使用具有以下设置的设计:

导轨:3.1,设计:1.3.4

我的模特:

class User::Account < ActiveRecord::Base  
  devise :database_authenticatable, :confirmable, :lockable, :recoverable,
         :rememberable, :registerable, :trackable, :timeoutable, :validatable,
         :token_authenticatable
end

路线:

Foco::Application.routes.draw do
  devise_for :users, :class_name => 'User::Account'

  root :to => 'main/cockpit#index', :constraints => lambda {|r| r.env["warden"].authenticate? }

  devise_scope :user do
    root :to => "devise/registrations#new"
  end
end

我的控制器:

class Main::CockpitController < ApplicationController
  before_filter :authenticate_user!

  def index
  end
end

谢谢!

【问题讨论】:

  • 如果你删除了 if user_signed_in?登录后调用 current_user.email 吗?
  • 不,'current_user' 的相同错误(未定义)=/
  • 您是否通过应用程序进行了身份验证?
  • 是的,我只能访问这个经过身份验证的控制器,你如何在路由中看到。我认为问题是在身份验证方法之前加载了布局,因为如果我可以重新加载 Ruby 代码,它就会运行。
  • 你能用你的解决方案创建一个答案并接受它吗?这样其他有类似问题的人都会从你的痛苦中受益:)

标签: ruby-on-rails devise ruby-on-rails-3.1


【解决方案1】:

我收到此错误是因为我在设计路线上做了一些奇怪的事情。让它们正常为我解决了这个问题。

【讨论】:

  • 绝对没用的答案。你的设计路线有“奇怪”的东西吗?是“奇怪”的技术术语吗,因为我不知道这个问题的真正答案......
【解决方案2】:

听起来你试图调用的函数没有定义。当您运行 rails g devise User 时,您将获得您正在尝试使用的功能,但是如果您使用非标准的东西,您需要使用它的单数版本。因此,如果您运行 rails g devise somethingnonstandard,您将拥有 somenonstandard_logged_in 功能?等等等等。

【讨论】:

  • 这是有道理的,但是我认为这是 Devise 的一个错误,因为当我将 :user 和 :class_name 设置为 'User::Account' 时,Devise 应该使用“user”生成所有范围方法。我会尝试你的替代方案,谢谢!
  • 好的,我尝试将所有 'user' 替换为 'account',然后是 'user_account',但没有任何效果,因为我使用的是 'User::Account' 之类的命名空间,我该怎么办? =/
  • 你给你的用户模型起什么名字?
  • 我的模型是 'User::Account'。奇怪的是,在开发环境重新加载后,ALL 运行良好。
  • 我终于解决了由'clear_helpers'方法引起的这个问题!
【解决方案3】:

ApplicationController 中的“clear_helpers”方法阻止了 Devise 加载他的助手。

所以我删除了这个方法并且应用程序正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    相关资源
    最近更新 更多