ERB vs HAML conversion of an if condition?
-
19-09-2019 - |
Question
I am getting started with HAML and am working on my converting my first file. The ostensibly correct omission of the "- end":
- 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
gets me:
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
While the logical
- 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
gets me:
You don't need to use "- end" in Haml. Use indentation instead:
- if foo?
%strong Foo!
- else
Not foo.
How do I get this conditional statement working in HAML?
Solution
HAML is indentation based, and the parser can be tricky. Replace
- 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
with
- 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
and give it a try. Notice how the indentation changed on the link_to lines.
OTHER TIPS
- 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
for more information about haml files vist
http://rubyonrails-tutor.blogspot.com/2010/02/hamltutorial-for-rails-developers.html
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow