Rails CanCanCan - 定义能力 - 使用结束语句和引用对象(模型或表名)

时间:2015-10-09 04:13:45

标签: ruby-on-rails conditional cancan cancancan

我正在尝试使用Rails 4制作应用程序。我将CanCanCan用于角色,使用Role_Model用于角色。

我很难找到条件陈述的基本规则。我不明白在'if / elsif / else'语句之后你需要多少'结束',以及为什么我要求更多结束语句会有很多错误。

这是我的能力.rb(我删除了一些方法的内容,使这个文件更短):

class Ability
  include CanCan::Ability


  def initialize(user)

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

      alias_action :create, :read, :to => :cr

      alias_action :create, :read, :update, :to => :cru

      alias_action :update, :destroy, :to => :ud

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

      new_registrant


      maintain_profile

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

      student_abilities

    elsif  user.try(:profile).present? && user.profile.has_role?(:educator)

      educator_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:researcher)

      researcher_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:ktp)
      ktp_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:faculty_manager)

      faculty_manager_abilities
      read_program_invitation

    elsif user.try(:profile).present? && user.profile.has_role?(:ip_asset_manager)

      ip_asset_manager_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:sponsor)
      sponsor_abilities
      read_program_invitation

    elsif user.try(:profile).present? && user.profile.has_role?(:project_manager)

      project_manager_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:representative)

     representative_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:grantor)

      grantor_abilities


    elsif user.try(:profile).present? && user.profile.has_role?(:investor)

      investor_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:adviser)

      adviser_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:innovation_consultant)

      innovation_consultant_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:participant)

      participant_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:guest)

      guest_abilities

    elsif user.try(:profile).present? && user.profile.has_role?(:manager)

      manager_abilities

    elsif user.admin?
        can :manage, :all
    end

  end


def new_registrant



      can :read, ActiveUserPolicy, [:last_login]
      can :read, PolicyPrivacy
      can :read, PolicyUser
      can :read, Newsletter

      cannot :read, Profile.total_privacy_control

问题1:PLURAL(用于DB表名或单个模型类名称

      cannot :read, Profiles.member_privacy_control
end


def maintain_profile
   new_registrant

   can :crud, Profiles, :user_id => user.id #[for themselves]

   can :read, Profiles
   cannot :read, Profile.total_privacy_control
   cannot :read, Profiles.member_privacy_control
   can :read, Feedbacks.visible 

end

def community_abilities
  can :ud, ArticleComments.own

end

def project_qanda
  # can ask questions on projects
      can :cr, ProjectQuestions if can? :read, Projects


    # can update & their own destroy  unanswered PQs
      can :ud, ProjectQuestions.user_question.unanswered 

    #can read answer to Q     
      can :read, ProjectAnswers, if can? :read, ProjectQuestions 

end

def program_qanda
end

def proposal_qanda
end

def student_abilities

    maintain_profile
    community_abilities
    project_qanda
    proposal_qanda

end

def educator_abilities

  maintain_profile
  community_abilities
  proposal_qanda

end


def researcher_abilities
  maintain_profile
  community_abilities 
  proposal_qanda 

end

def ktp_abilities
  maintain_profile
  community_abilities
  proposal_qanda

end

def faculty_manager_abilities
  maintain_profile
  community_abilities

end

def ip_asset_manager_abilities
  maintain_profile
  community_abilities
  #answer diligence questions

end

def sponsor_abilities
  maintain_profile
  community_abilities
  project_qanda
  program_qanda
  # create diligence report
end


def representative_abilities
  maintain_profile
  community_abilities
end

def grantor_abilities
  maintain_profile
  community_abilities

 end

def grant_application_abilities
end



def investor_abilities
end

def adviser_abilities
end

def innovation_consultant_abilities
  maintain_profile
  community_abilities
end

def participant_abilities
  maintain_profile
  community_abilities

end

def guest_abilities
  maintain_profile

end

def manager_abilities


end

def transaction_abilities
end

def diligience_abilities

end

def job_creator
end

def job_application
end

def feedback_abilities
end

def project_manager_abilities
  maintain_profile

end
end

问题2:我添加了所有以下结束语,因为我保留了语法错误,意味着意外的输入结束,期望关键词结束。我知道,如果错误停止了,我会一个接一个地添加,但我不理解这些陈述的结束或者为什么需要它们。这个文件中没有任何相同的数字。我如何找到关于何处提交终止声明的规则?

  end
  end
  end
  end
 end
  end
  end
  end 
  end
  end
  end
  end
  end

1 个答案:

答案 0 :(得分:0)

它将解决您的问题2 检查你的if else语法。 它应该是这样的

If..Elsif...end语法:

if conditional [then]
      code...
[elsif conditional [then]
      code...]...
[else
      code...]
end

实施例

if var == 10
  print “Variable is 10”
elsif var == “20”
  print “Variable is 20”
else
  print “Variable is something else”
end