带有HAML的Rails Flash消息 [英] Rails flash messages with HAML

查看:51
本文介绍了带有HAML的Rails Flash消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习HAML:并且我无法将Flash块翻译为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 %>

推荐答案

在这里:

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

仅供参考,您可以通过在声明之后将属性附加为散列来将属性添加到任何元素.如果您不指定元素,而仅指定类或ID,则HAML会将该元素设为具有给定类或ID的div.但是,您可以通过多种方式执行此操作.例如,它们都是相同的:

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

所有输出:<div class="foo bar" id="test"></div>

不过,您需要将计算出的属性放在哈希中,即:

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

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

输出:<div class="bar"></div>

此外,请注意,在上述所有示例中,:attribute => 'value'都可以表示为attribute: 'value',例如:

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

希望有帮助.

这篇关于带有HAML的Rails Flash消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆