我正在尝试使用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
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
end
end
end
end
end
end
end
end
end
end
end
end
end
答案 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