Rails 设计榜样和 CanCanCan - 定义能力

Rails Devise Role Model & CanCanCan - defining abilities

我正在尝试在 CanCanCan 中定义能力。

我不知道开始的语法。

我为我的角色使用角色模型,角色在我的 Profile.rb 中定义。 Profile.rb 属于 User.rb.

我正在尝试检查用户是否具有角色 :student。

当我尝试时:

if {user_signed_in?, user.profile.has_role? :student}

我收到一个语法错误:

syntax error, unexpected ',', expecting =>
    if {user_signed_in?, user.profile.has_role? :student}

当我尝试时:

if {user_signed_in? && user.profile.has_role? :student}

我收到一个语法错误:

syntax error, unexpected tSYMBEG, expecting =>
    if {user_signed_in? && user.profile.has_role? :student}

我也试过用普通括号替换花括号并完全删除它们。

当我尝试删除设计部分 (user_signed_in) 并使用下面评论中的建议时,我尝试:

if  user.profile.has_role?(:student) 

我得到这个错误:

undefined method `has_role?' for nil:NilClass

当我尝试时:

if  user.profile.has_role? :student 

我收到这个错误:

undefined method `has_role?' for nil:NilClass

当我尝试时:

if  user.profile.has_role?(student) 

我收到这个错误:

undefined local variable or method `student' for #<Ability:0x007fd034a78968>

我在 profile.rb 中定义了以下角色:

  roles :admin, :manager, #
        :student, :educator, :researcher, :ktp, :faculty_manager, :ip_asset_manager,  # for universities
        :sponsor, # for industry
        :project_manager, :representative, # for both uni and industry
        :grantor, :investor, :adviser, :innovation_consultant, :panel_member, # external
        :participant, :guest # public

当我尝试时:

can :crud, Profile, :user_id => user.id if  user.profile.has_role? :student

我没有收到任何错误,但我对这种方法的问题是学生可以做很多事情(有 10 行权限,所以我需要将 if 语句分别添加到 10 'can' 语句,除非有一种方法可以在下一个 'elsif' 语句之前将 if 语句应用于所有行。

我的ability.rb的第一部分贴在下面(角色很多,能力也很多,所以没贴全)。

class Ability
  include CanCan::Ability

  def initialize(user)

      alias_action :create, :read, :update, :destroy, :to => :crud


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

      #users who are not signed in can create registration or login 

      # can read publicly available projects, programs and proposals
      can :read, Project, {:active => true, :closed => false, :sweep => { :disclosure => { :allusers => true } } }

      # {:active => true, :closed => false  &&  :Project.sweep.disclosure.allusers => true}
      # if user role is student

      can :crud, Profile, :user_id => user.id if  user.profile.has_role? :student #[for themselves]
      can :read, ProjectInvitation, {:student_id => @current_user && :expiry_date_for_students_to_claim_project > Time.now}
      #  can read project invitations addressed to them
      # can read projects to which they are invited whilst the invite is available to accept;
      can :read, Project, {} #if invited to the project?
      # and they can create responses to invitations to those projects
      can :update, ProjectInvitation.student_accepted
      # they can create questions on those projects before the invite expiry date;
      can :read, ProjectQuestion, {} #if intvited
      can [:create, :update, :destroy], ProjectQuestion #if they created the question
      can :read, ProjectAnswer #if its on a project they can see
      # they can update term sheets and template agreements for those projects
      can [:read, :update], TermSheet #{where created for project in which they are participating}
      can [:read, :update], ProjectAgreement #{where created for a project in which they are participating}
      can [:read, :update], FinanceAgreement #{where created for a project in which they are participating}
      can [:read, :update], Nda #{where created for a project in which they are participating}
      can [:create, :update], Feedback #{where created for a project in which they are participating and the feedback is on other project team members and the project is completed}


    elsif user.profile.has_role? :educator

当我尝试时(下面的建议):

if  user.try(:profile).present? && user.profile.has_role? :student 

我收到这个错误:

syntax error, unexpected tSYMBEG, expecting keyword_then or ';' or '\n'
...nt? && user.profile.has_role? :student

有人能看出我做错了什么吗?

以下代码

if {user_signed_in?, user.profile.has_role? :student}
if {user_signed_in? && user.profile.has_role? :student}
if {user_signed_in? && user.profile.has_role? :student}

您不能将 {} 用于 ruby 语句,它需要一个键值对。您可以按以下方式重写代码

if user_signed_in? && user.profile.has_role? :student

但是你得到了一个空指针错误,所以你已经在你的代码中修复它如下,首先检查是否 user.try(:profile).present? 然后你可以调用 user.profile.has_role? :student 因为你的 profile 得到nil.

if user.try(:profile).present? && user.profile.has_role?(:student)