Haml, ruby on rails error: Encountered a syntax error while rendering template

Haml, ruby on rails error: Encountered a syntax error while rendering template

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

"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 ! 

我该如何解决?

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

%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 中。这些额外的 end 会给您带来 unexpected end, expecting end-of-input 语法错误。