我需要在HAML代码中进行以下构造:

- if @organisation.type == 'Company'
%p
  %strong Number of Employees:
  = @organisation.number_of_employees
- elsif @organisation.type == 'EducationalInstitution'
%p
  %strong Number of Students
  = @organisation.number_of_students

但是我收到了一个语法错误:没有前面的“if”就得到了“elsif”
我必须如何更新代码以解决错误?

最佳答案

您的缩进看起来可能是问题所在

- if @organisation.type == 'Company'
  %p
    %strong Number of Employees:
    = @organisation.number_of_employees
- elsif @organisation.type == 'EducationalInstitution'
  %p
    %strong Number of Students
    = @organisation.number_of_students

奖金缩进怪癖

如果注释不正确,则if / else语句也会失败,该注释也将遵循正确的缩进。

例如
- if @organisation.type == 'Company'
  %p
    %strong Number of Employees:
    = @organisation.number_of_employees

-# Institutional case
- elsif @organisation.type == 'EducationalInstitution'
  %p
    %strong Number of Students
    = @organisation.number_of_students

将失败。哪里
- if @organisation.type == 'Company'
  %p
    %strong Number of Employees:
    = @organisation.number_of_employees

- elsif @organisation.type == 'EducationalInstitution'
  -# Institutional case
  %p
    %strong Number of Students
    = @organisation.number_of_students

将是正确的。

关于ruby-on-rails - HAML:没有前面的“if”就得到了“elsif”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32865581/

10-13 00:28