如何使用Haml查看能力CanCan?

时间:2013-12-24 18:01:54

标签: ruby ruby-on-rails-4 haml cancan

尝试检查视图中的异能:

...
%td= user.zip
  %td 
    - if can? :manage, user
      %td= puts "LOLo!"
...

在以下情况下提出错误:

语法错误,意外的keyword_else,期待keyword_end

文件能力.rb:

class Ability
include CanCan::Ability

def initialize(user)
  # Define abilities for the passed in user here. For example:
  user ||= User.new # guest user (not logged in)
  if user.admin?
    can :manage, :all
  else user.editor?
    can :edit, :all
  else
    can :read, :all
  end
end
end

2 个答案:

答案 0 :(得分:2)

这将完全适用于HAML。您的initialize方法有两个else条件 - 一个必须是elsif

def initialize(user)
  # Define abilities for the passed in user here. For example:
  user ||= User.new # guest user (not logged in)

  if user.admin?
    can :manage, :all
  elsif user.editor?  # Problem is here
    can :edit, :all
  else
    can :read, :all
  end
end

答案 1 :(得分:0)

决定。我没有实现方法admin?,但我有另一个role?。将user.admin?更改为user.role?