undefined方法current_user:Cancan和Active Scaffold

时间:2010-11-16 00:02:40

标签: ruby-on-rails permissions cancan scaffold

我正在尝试使用

向活动脚手架控制器添加操作链接
if current_user.can? :update, Post
  config.action_links.add 'export', :label => 'Export', :page => true
end

但无论我尝试使用哪种控制器,我总是得到undefined method current_user
那么,我该如何检查记录的用户是否可以做某事? 我甚至试过

def ability
  @ability ||= Ability.new(self)
end
delegate :can?, :cannot?, :to => :ability

建议here,但没有成功。
这有什么问题?

2 个答案:

答案 0 :(得分:2)

请注意,在正常情况下,ActiveScaffold配置是在类中完成的,而不是实例,因此没有设置current_user,因为还没有请求。

答案 1 :(得分:1)

current_user是一种通常定义的方法,但它是您必须添加的方法。它不是由铁轨提供的。因此,您需要在ApplicationController中使用current_user方法。有很多例子,但我会看一下authlogic example app on github

相关问题