簡體   English   中英

Rails Devise角色模型和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個“可以”語句中的每一個中,除非有一種方法可以將if語句應用於下一個'elsif'語句之前的所有行。

我的bility.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 user.profile.has_role? :student因為您的profile nil

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM