# Haml says yes, rails says no

**URL:** <https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768>\
**Category:** Rails\
**Created:** [August 25, 2008, 10:50am UTC](https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768 "2008-08-25T10:50:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 25, 2008, 10:50am UTC](https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768/1 "2008-08-25T10:50:28Z")

</div>

I’m using the haml gem and it’s great, been using it for a while and I  
love it.

I’ve got a problem trying to display the flash messages, if I do the  
following:

```
 - if flash[:notice]
   #flashNotice
     =flash[:notice]
 - else if flash[:error]
   #flashError
     =flash[:error]
 - end

```

I get

You don’t need to use “- end” in Haml. Use indentation instead:

- if foo?  
%strong Foo!
- else  
Not foo.

Ok, so I take out “- end” and I get

default.haml:21: syntax error, unexpected $end, expecting kEND

So which is it? What do I have to do to get around this? My  
indentation is perfect, so I don’t see what else I can do.  
Incidentally this appears to be a new problem with the latest version  
of haml. Before I was using “- end” and it worked perfectly.

Any help appreciated.

Thanks

Matt

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 25, 2008, 11:14am UTC](https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768/2 "2008-08-25T11:14:39Z")

</div>

I realize this isn’t a direct answer to your question but to give you  
some context I have been using haml since it was first released and I  
have used in on many many projects. I absolutely cannot go back to rhtml  
because haml is so much more concise. Still though, I have no idea the  
answer to your question. Why? Because I had always heard that  
conditional logic should not be in my views. I realize this is a  
personal opinion but I figure I would show you how I would handle this:

# application\_helper.rb

# even if flash error doesn’t exist this will return nil (empty as

expected)  
def place\_flash\_message  
flash[:notice] ? flash[:notice] : flash[:error]  
end

# view.html.haml

= place\_flash\_message

or if I really need conditional logic such as in:

if logged\_in?  
something  
else  
other thing

I would simply create a helper or a helper block with capture.

Anyways, in my opinion this improves readability and as a general rule  
of thumb in 2 years of rails development projects I have never felt the  
need to stick conditional logic directly in my view.

Now assuming you discard all that, I think the correct method is not to  
put the end in haml which would make more sense in the haml way of doing  
things. Since I haven’t actually needed to do this, I am not sure the  
issue. Good luck

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 25, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768/3 "2008-08-25T15:10:49Z")

</div>

Matt H. wrote:

> ```
> - if flash[:notice]
> #flashNotice
> =flash[:notice]
> - else if flash[:error]
> #flashError
> =flash[:error]
> - end
> 
> ```

What about

- if flash[:notice]  
#flashNotice  
= flash[:notice]
- elsif flash[:error]
# flashError
= flash[:error]

Your “else if” may be constructing an inner “if” whose indentation  
doesn’t resolve properly

---

<div class="post-metadata">

**Author:** ![bouldr-matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/85e7bf/32.png) [@bouldr-matt](https://www.ruby-forum.com/u/bouldr-matt)\
**Post date:** [August 25, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/haml-says-yes-rails-says-no/146768/4 "2008-08-25T15:28:25Z")

</div>

Ar Chron wrote:

> What about
> 
> - if flash[:notice]  
> #flashNotice  
> = flash[:notice]
> - elsif flash[:error]
> # flashError
> = flash[:error]
> 
> Your “else if” may be constructing an inner “if” whose indentation  
> doesn’t resolve properly

Yes elsif solves it, I must’ve been using too many other languages  
recently 😃

Thanks

Matt
