Вопрос

Trying to check abilities in view like this:

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

Raise an error in line if:

syntax error, unexpected keyword_else, expecting keyword_end

File abilities.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
Это было полезно?

Решение

This will totally work in HAML. Your initialize method has two else conditions - one needs to be an 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

Другие советы

Decided. I didn'n implement method admin?, but I have another one role?. Change user.admin? to user.role?.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top