ERB与HAML转换if条件?

时间:2009-07-28 03:29:25

标签: ruby-on-rails haml erb

我开始使用HAML并正在处理我的第一个文件。 表面上正确地省略了“ - 结束”:

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path

得到我:

app/views/layouts/application.html.haml:28: syntax error, unexpected kENSURE, expecting kEND
app/views/layouts/application.html.haml:30: syntax error, unexpected $end, expecting kEND

虽然合乎逻辑

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path
- end

得到我:

You don't need to use "- end" in Haml. Use indentation instead:
- if foo?
  %strong Foo!
- else
  Not foo.

如何在HAML中使用此条件语句?

3 个答案:

答案 0 :(得分:21)

HAML是基于缩进的,解析器可能很棘手。取代

- if current_user
= link_to 'Edit Profile', edit_user_path(current_user.id)
= link_to 'Logout', logout_path
- else
= link_to 'Register', new_user_path
= link_to 'Login', login_path

- if current_user
  = link_to 'Edit Profile', edit_user_path(current_user.id)
  = link_to 'Logout', logout_path
- else
  = link_to 'Register', new_user_path
  = link_to 'Login', login_path

并尝试一下。注意如何在link_to行上更改缩进。

答案 1 :(得分:3)

- if current_user
  = link_to 'Edit Profile', edit_user_path(current_user.id)
  = link_to 'Logout', logout_path
- else
  = link_to 'Register', new_user_path
  = link_to 'Login', login_path

答案 2 :(得分:1)