首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails设计角色模型& CanCanCan定义能力

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

Stack Overflow用户
提问于 2015-09-02 06:02:32
回答 1查看 434关注 0票数 0

我试图在CanCanCan中定义能力。

我搞不懂要开始的语法。

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

我正在试着检查用户是否有这个角色:学生。

当我尝试:

代码语言:javascript
运行
复制
if {user_signed_in?, user.profile.has_role? :student}

我收到一个语法错误,上面写着:

代码语言:javascript
运行
复制
syntax error, unexpected ',', expecting =>
    if {user_signed_in?, user.profile.has_role? :student}

当我尝试:

代码语言:javascript
运行
复制
if {user_signed_in? && user.profile.has_role? :student}

我收到一个语法错误,上面写着:

代码语言:javascript
运行
复制
syntax error, unexpected tSYMBEG, expecting =>
    if {user_signed_in? && user.profile.has_role? :student}

我还试着用普通括号替换花括号,并将它们全部去掉。

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

代码语言:javascript
运行
复制
if  user.profile.has_role?(:student) 

我得到了一个错误:

代码语言:javascript
运行
复制
undefined method `has_role?' for nil:NilClass

当我尝试:

代码语言:javascript
运行
复制
if  user.profile.has_role? :student 

我知道这个错误:

代码语言:javascript
运行
复制
undefined method `has_role?' for nil:NilClass

当我尝试:

代码语言:javascript
运行
复制
if  user.profile.has_role?(student) 

我知道这个错误:

代码语言:javascript
运行
复制
undefined local variable or method `student' for #<Ability:0x007fd034a78968>

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

代码语言:javascript
运行
复制
  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

当我尝试:

代码语言:javascript
运行
复制
can :crud, Profile, :user_id => user.id if  user.profile.has_role? :student

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

我的ability.rb的第一部分贴在下面(有很多角色和能力,所以我还没有贴上整件东西)。

代码语言:javascript
运行
复制
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

当我尝试(以下建议)时:

代码语言:javascript
运行
复制
if  user.try(:profile).present? && user.profile.has_role? :student 

我知道这个错误:

代码语言:javascript
运行
复制
syntax error, unexpected tSYMBEG, expecting keyword_then or ';' or '\n'
...nt? && user.profile.has_role? :student

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-02 06:36:30

用于下列代码

代码语言:javascript
运行
复制
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语句,它需要一个键值对。您可以按照以下方式重写代码

代码语言:javascript
运行
复制
if user_signed_in? && user.profile.has_role? :student

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

代码语言:javascript
运行
复制
if user.try(:profile).present? && user.profile.has_role?(:student)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32345660

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档