Rails - 在 if/elsif/else 语句中写入 'end' 的次数
Rails - how many times to write 'end' in an if/elsif/else statement
我在想办法 Rails 4.
我正在使用 CanCanCan 来获取能力,但在 ability.rb 中出现错误。
相关错误认为我在下面的块中有一个额外的'end'?
def program_qanda
# can ask questions on programs
# TO DO: create this
can :cr, ProgramQuestions if can? :read, Programs
end
can :read, ProgramAnswers, if can? :read, ProgramQuestions
end
can :ud, ProgramQuestions.user_question.unanswered
#can read answer to Q
end
我看不到那会在哪里,我定义了另一个方法(以类似的格式,在这个方法之上,并且没有出现引用那个方法的错误。
错误的行引用指向该块中的最后一端。
这让我想知道行引用是否可能是一个错误,并且问题从我定义角色的更高层开始。相关的初始化语句是:
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
if user.try(:profile).present? && user.profile.has_role?(:pending)
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?(:adviser)
adviser_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
else user.admin?
can :manage, :all
end
end
我需要在这个块中有更多的结束语句吗?规则是什么?每个 'if' 需要一个 'end',每个 'else' 需要一个末端,每个 'elsif' 需要一个末端吗?我认为 'if' 的一端和顶部的 def 的一端就足够了。
找不到对此的引用 - 所有示例仅显示语句,而不显示 'ifs' 是否适合包含所有额外 'end' 语句的块。
can :cr, ProgramQuestions if can? :read, Programs
这是一个内联条件,它转换为:
if can? :read, Programs
can :cr, ProgramQuestions
end
内联条件仅适用于同一行中它之前的代码,不需要end
。所以,你没有额外的 end
- 你有太多 end
s.
代码
else user.admin?
can :manage, :all
end
无效。你可能想要:
elsif user.admin?
can :manage, :all
end
编辑:
此代码中有大量内容不太正确。我认为很大一部分可以通过格式化来解决。其他部分需要调整为有效语法。如果您使用 if 限定带有参数的方法调用,最好用括号将参数括起来。另外,您还有一个逗号。将上面的代码更改为如下所示:
def program_qanda
# can ask questions on programs
# TO DO: create this
can( :cr, ProgramQuestions ) if can?( :read, Programs )
can( :read, ProgramAnswers ) if can?( :read, ProgramQuestions )
can( :ud, ProgramQuestions.user_question.unanswered )
#can read answer to Q
end
然后检查整个 class,边检查边更正格式和语法。使用 IDE 可能对学习过程有帮助。我个人喜欢 RubyMine(没有隶属关系,只是一个很棒的产品)。它具有自动格式化功能并突出显示语法错误,仅举几个您可能感兴趣的功能。
我在想办法 Rails 4.
我正在使用 CanCanCan 来获取能力,但在 ability.rb 中出现错误。
相关错误认为我在下面的块中有一个额外的'end'?
def program_qanda
# can ask questions on programs
# TO DO: create this
can :cr, ProgramQuestions if can? :read, Programs
end
can :read, ProgramAnswers, if can? :read, ProgramQuestions
end
can :ud, ProgramQuestions.user_question.unanswered
#can read answer to Q
end
我看不到那会在哪里,我定义了另一个方法(以类似的格式,在这个方法之上,并且没有出现引用那个方法的错误。
错误的行引用指向该块中的最后一端。
这让我想知道行引用是否可能是一个错误,并且问题从我定义角色的更高层开始。相关的初始化语句是:
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
if user.try(:profile).present? && user.profile.has_role?(:pending)
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?(:adviser)
adviser_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
else user.admin?
can :manage, :all
end
end
我需要在这个块中有更多的结束语句吗?规则是什么?每个 'if' 需要一个 'end',每个 'else' 需要一个末端,每个 'elsif' 需要一个末端吗?我认为 'if' 的一端和顶部的 def 的一端就足够了。
找不到对此的引用 - 所有示例仅显示语句,而不显示 'ifs' 是否适合包含所有额外 'end' 语句的块。
can :cr, ProgramQuestions if can? :read, Programs
这是一个内联条件,它转换为:
if can? :read, Programs
can :cr, ProgramQuestions
end
内联条件仅适用于同一行中它之前的代码,不需要end
。所以,你没有额外的 end
- 你有太多 end
s.
代码
else user.admin?
can :manage, :all
end
无效。你可能想要:
elsif user.admin?
can :manage, :all
end
编辑:
此代码中有大量内容不太正确。我认为很大一部分可以通过格式化来解决。其他部分需要调整为有效语法。如果您使用 if 限定带有参数的方法调用,最好用括号将参数括起来。另外,您还有一个逗号。将上面的代码更改为如下所示:
def program_qanda
# can ask questions on programs
# TO DO: create this
can( :cr, ProgramQuestions ) if can?( :read, Programs )
can( :read, ProgramAnswers ) if can?( :read, ProgramQuestions )
can( :ud, ProgramQuestions.user_question.unanswered )
#can read answer to Q
end
然后检查整个 class,边检查边更正格式和语法。使用 IDE 可能对学习过程有帮助。我个人喜欢 RubyMine(没有隶属关系,只是一个很棒的产品)。它具有自动格式化功能并突出显示语法错误,仅举几个您可能感兴趣的功能。