将哈希初始化存储到变量

时间:2012-12-31 08:53:41

标签: ruby sinatra rack

我的Sinatra应用程序中有一段代码

    - if flash[:error]
      .alert.alert-error
        %button.close{:type =>" button", "data-dismiss"=>"alert"}x
        = flash[:error]
    - if flash[:warning]
      .alert.alert-warning
        %button.close{:type =>" button", "data-dismiss"=>"alert"}x
        = flash[:warning]
    - if flash[:info]
      .alert.alert-info
        %button.close{:type =>" button", "data-dismiss"=>"alert"}x
        = flash[:info]

有什么方法可以简单吗?特别是,有没有办法通过将它们存储为变量来摆脱重复:type =>" button", "data-dismiss"=>"alert"哈希初始化?

1 个答案:

答案 0 :(得分:1)

尝试类似:

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