0

我正在尝试在 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

请问有人可以看到我做错了什么吗?

4

1 回答 1

0

对于以下代码

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)
于 2015-09-02T06:36:30.153 回答