Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails flash messages with HAML

i started learn HAML: and i can't translate flash block to HAML:

<% flash.each do |key, value| %>
  <div class="alert alert-<%= key %>">
    <button type="button" class="close" data-dismiss="alert">×</button>
    <strong><%= value %></strong>
  </div>        
<% end %>
like image 793
Dmytro Vasin Avatar asked Dec 20 '12 19:12

Dmytro Vasin


1 Answers

Here you go:

= flash.each do |key, value|
  .alert{ :class => "alert-#{key}" }
    %button.close{ :data => { :dismiss => "alert" } } x
    %strong
      = value

Just FYI you can add attributes to any element by attaching them as a hash after the declaration. If you don't specify an element, just a class or ID, HAML makes that element a div with the given class or id. But you could do this many ways. For instance, these are all the same:

%div{:class => 'foo bar', :id => 'test' }
.foo{:class => 'bar', :id => 'test'}
#test.bar{:class => 'foo'}
#test.foo.bar

All output: <div class="foo bar" id="test"></div>

You need to put computed attributes in the hash though, i.e.:

- klass = "bar"
%div{ :class => klass }

Outputs: <div class="bar"></div>

Also, note that in all the examples above, the :attribute => 'value' can be expressed as attribute: 'value', e.g.:

%button.close{ data: { dismiss: 'alert' } } x

Hope that helps.

like image 117
Andrew Avatar answered Oct 16 '22 13:10

Andrew