与HAML折叠手风琴时出现意外错误

时间:2015-12-16 12:02:54

标签: ruby-on-rails haml bootstrapping

我在Ruby on Rails上使用Collapse bootstrap开发了网页。 有了Haml,我尝试了如下。 但是有一些意想不到的错误。

.accordion-group
    .accordion-heading
      %according-toggle{"data-toggle" => "collapse", "data-target"=>"#{heading}"}
        = link_to heading, "#{heading}"
    .accordion-body.collapse.in{:id => "#{heading}"}
      .accordion-inner
        -items.each do |item|
        %li{ :class => (params[:controller] == item[:controller] ? "active" : "") }
          = link_to item[:path] do
            - if item[:data_icon]
              %span{ :class => "nav-icon", :data => {"icon" => "#{item[:data_icon]}".html_safe}}
              %span{ :class => "nav-text"}
                = "#{item[:name]}"
            - else
              = "#{item[:name]}"

2 个答案:

答案 0 :(得分:0)

您的缩进似乎不正确,可以查看items.each下面的行吗? 应该在循环内部。

.accordion-group
  .accordion-heading
    %according-toggle{"data-toggle" => "collapse", "data-target"=>"#{heading}"}
      = link_to heading, "#{heading}"
  .accordion-body.collapse.in{:id => "#{heading}"}
    .accordion-inner
      - items.each do |item|
        %li{ :class => (params[:controller] == item[:controller] ? "active" : "") }
          = link_to item[:path] do
            - if item[:data_icon]
              %span{ :class => "nav-icon", :data => {"icon" => "#{item[:data_icon]}".html_safe}}
              %span{ :class => "nav-text"}
                = "#{item[:name]}"
            - else
              = "#{item[:name]}"

答案 1 :(得分:0)

我已按如下方式更改了代码。 原因是标题包括空间和特殊字符,如'&'。 所以我用下划线替换它们。 之后它正在运作。

.panel-group{:id=>"accordion"}
  - groups.each do |heading, items|
  .panel.panel-default
    .panel-heading
      %h4.panel-title
        %a.accordion-toggle{"data-toggle" => "collapse", "data-parent"=>"#accordion",:href=>"#"+"#{heading}".gsub(/\W/,'_')}
          = raw heading
    .panel-collapse.collapse{:id=>"#{heading}".gsub(/\W/,'_')}
      .panel-body
        %ul.nav.nav-pills.nav-stacked
          - items.each do |item|
            %li{:class => (params[:controller] == item[:controller] ? "active" : "") }
              = link_to item[:path] do
                - if item[:data_icon]
                  %span{ :class => "nav-icon", :data => {"icon" => "#{item[:data_icon]}".html_safe}}
                  %span{ :class => "nav-text"}
                  = "#{item[:name]}"
                - else
                  = "#{item[:name]}"