在HAML上双重打印实例变量

时间:2012-04-27 23:50:22

标签: ruby-on-rails ruby haml

我遵循本教程(http://ruby.railstutorial.org/chapters/sign-up#sec:signup_form)。但是,当代码在清单7.23 (转换为haml)

=if @user.errors.any?
%div{:id => "error_explanation"}
  %div{:class=>"alert alert-error"}
    il modulo contiene errori
    %ul
      = @user.errors.full_messages.each do |msg|
        %li
          = msg

我最终得到了双打印输出:预期的打印输出,然后是变量的打印输出

Name can't be blank
Password is too long (maximum is 15 characters)
["Name can't be blank", "Password is too long (maximum is 15 characters)"]
<h1>Registrazione nuovo utente</h1> <div id='error_explanation'> <div class='alert alert-error'> il modulo contiene errori <ul> <li> Name can't be blank </li> <li> Password is too long (maximum is 15 characters) </li> [&quot;Name can't be blank&quot;, &quot;Password is too long (maximum is 15 characters)&quot;]</ul> </div> </div> 

我对rails编程非常陌生,但我真的无法获得。

谢谢,Marcello

1 个答案:

答案 0 :(得分:2)

您的if声明应以-开头,而不是=

- if @user.errors.any?

你的循环也应该如此:

- @user.errors.full_messages.each do |msg|

通常,如果您尝试将erb转换为haml,则<% ... %>变为- ...<%= ... %>变为= ...