【问题标题】:Haml, ruby on rails error: Encountered a syntax error while rendering templateHaml,ruby on rails 错误:渲染模板时遇到语法错误
【发布时间】:2019-10-15 17:39:28
【问题描述】:

我尝试执行以下代码,但得到了:

"Encountered a syntax error while rendering template:"

这是我的看法:

%section
    %article
        - if @toss % 2 === 0
            %p the player #{@player_one.name} start the fight !
        - else 
            %p the player #{@player_two.name} start the fight !

        - while @hp_player_one > 0 && @hp_player_two > 0 
            - @hp_player_one -=  @player_two.attack
                %p  There is only #{@hp_player_one.to_s} point to #{@player_one.name}
            - @hp_player_two -=  @player_one.attack
                %p  There is only #{@hp_player_two.to_s} point to #{@player_two.name}
                -if @hp_player_one <= 0 && @hp_player_two > 0
                    %p #{@player_one.name} lost 
                -elsif @hp_player_two <= 0 && @hp_player_one > 0
                    %p #{@player_two.name} lost 
                -else 
                    %p draw ! 

我该如何解决?

【问题讨论】:

  • %p draw ! 看起来很可疑
  • 看起来你已经用制表符而不是双空格缩进了。
  • @Kris 是的,但是 %p 抽奖出了什么问题?

标签: ruby ruby-on-rails-3 haml


【解决方案1】:

你的缩进是错误的,我想你想要这样的东西:

%section
    %article
        - if @toss % 2 === 0
            %p the player #{@player_one.name} start the fight !
        - else 
            %p the player #{@player_two.name} start the fight !

        - while @hp_player_one > 0 && @hp_player_two > 0 
            - @hp_player_one -=  @player_two.attack
            %p  There is only #{@hp_player_one.to_s} point to #{@player_one.name}
            - @hp_player_two -=  @player_one.attack
            %p  There is only #{@hp_player_two.to_s} point to #{@player_two.name}
            -if @hp_player_one <= 0 && @hp_player_two > 0
                %p #{@player_one.name} lost 
            -elsif @hp_player_two <= 0 && @hp_player_one > 0
                %p #{@player_two.name} lost 
            -else 
                %p draw ! 

特别是你不应该在像这样的行之后缩进

- @hp_player_one -=  @player_two.attack

因为这不会开始一个块。 Haml 看到这一行之后的缩进并假定它是一个块的开始,因此将相应的end 插入到生成的 Ruby 中。这些额外的ends 会给你unexpected end, expecting end-of-input 语法错误。

【讨论】:

    猜你喜欢
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    相关资源
    最近更新 更多