Rails flash messages with HAML

后端 未结 1 1979
盖世英雄少女心
盖世英雄少女心 2021-02-07 20:46

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

<% flash.each do |key, value| %>
  
\">
相关标签:
1条回答
  • 2021-02-07 21:29

    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.

    0 讨论(0)
提交回复
热议问题