【问题标题】:current_user method not found (Rails, Devise, Cancan)未找到 current_user 方法(Rails、Devise、Cancan)
【发布时间】:2013-03-15 23:39:01
【问题描述】:

我将 Rails 3.2、Devise 和 Cancan 用于具有 3 个用户类别的应用程序:公司、客户和管理员。

我的问题是我得到了这个:

undefined local variable or method `current_user' for #<HomeController:0xb4fd217c>

在 HomeController 我有:

class HomeController < ApplicationController
  before_filter :authenticate_customer!
  load_and_authorize_resource :class => false

  def index
        render 'index'
    end

end

另外,在视图中:

- content_for :title, 'Home'
= link_to('Logout', destroy_customer_session_path, :method => :delete)

我没有在代码中的任何地方调用 current_user。

有什么想法吗?

【问题讨论】:

  • 您的“authenticate_customer!”是什么?方法是什么样的?
  • 这是设计的,我没有实现。
  • 您使用的是 CanCan 还是其他与 Devise 交互的 gem?我特别提到 CanCan 是因为您可能必须根据 this GitHub issue 在您的 ApplicationController 中创建一个 current_ability 方法。
  • 请注意,一般情况下,引用current_user 的任何地方都需要更改为current_customer,但您说您没有在任何地方引用current_user。

标签: ruby-on-rails devise cancan


【解决方案1】:

CanCan 对您的应用程序做了两个假设。

  • 您有一个定义权限的能力类。
  • 控制器中有一个 current_user 方法,它返回当前用户模型。

您可以通过在 ApplicationController 中定义 current_ability 方法来覆盖这两者。

https://github.com/ryanb/cancan/wiki/Changing-Defaults

【讨论】:

  • 我会感谢一些反馈。我的回答解决了你的问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 2012-09-09
  • 1970-01-01
  • 2013-08-22
相关资源
最近更新 更多