Rails设计角色模型& CanCanCan - 定义能力

时间:2015-09-02 06:02:33

标签: ruby-on-rails cancan user-roles cancancan

我正在尝试在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'中的每一个'语句,除非有一种方法可以在下一个'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

请有人看到我做错了吗?

1 个答案:

答案 0 :(得分: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 1}}。

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