Rails flash messages with HAML

﹥>﹥吖頭↗ 提交于 2019-12-03 16:34:27

问题


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 %>

回答1:


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.



来源:https://stackoverflow.com/questions/13978891/rails-flash-messages-with-haml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!