views:

1957

answers:

3

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?

+5  A: 

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.

Mike Trpcic
"Inconsistent indentation: 5 spaces were used for indentation, but the rest of the document was indented using 2 spaces."It didn't like it. :(
You're indenting something too many spaces - or too few. Can you post your entire template?
nex3
I took another look at the spacing and I think you were right. I think I fixed the indenting but now I am getting this:/app/models/user_session.rb:5: syntax error, unexpected '<'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ^/app/models/user_session.rb:8: syntax error, unexpected '<'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> ^/app/models/user_session.rb:8: syntax error, unexpected tIDENTIFIER, expecting $end<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> ^What am I missing?
That doesn't appear to be related to Haml. It sounds like you've somehow accidentally put HTML into a model class file.
Chuck
@Chuck: HAML isn't used for models, it's used for Views.@sleepycat: You have HTML in your HAML file. Please do some further reading into HAML and find some examples.
Mike Trpcic
Actually, as far fetched as it was, Chuck nailed it. I had tried to save a model file from github and for some reason just got a bunch of html instead of any ruby code. What a mess!The HAML stuff in the views was a little messed up too but I sorted it out with the tips you guys gave. Thanks to everyone!
+1  A: 
- 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
Lichtamberg
A: 

for more information about haml files vist

http://rubyonrails-tutor.blogspot.com/2010/02/hamltutorial-for-rails-developers.html

mathewa99