简体   繁体   English

Bootstrap / Rails-手风琴内部的字符串插值

[英]Bootstrap/Rails - String Interpolation inside accordion

I've been struggling with string interpolation inside an HTML accordion. 我一直在努力在HTML手风琴中进行字符串插值。 It's more a logic issue that I can't seem to figure out (fairly new to rails). 我似乎无法弄清楚这是一个逻辑问题(对Rails来说是相当新的)。 The problem I have is that while the layout is fine, when I click to collapse any of the 3 collapsible panels they all collapse. 我的问题是,虽然布局很好,但是当我单击以折叠3个可折叠面板中的任何一个时,它们全部都折叠了。 (Because I only have 1 #collapse). (因为我只有1#崩溃)。

Outermost ERB 最外层的ERB

<div class="container-fluid">
    <div class="row">
        <div class="col-md-2 col-sm-3">
                <h2>Categorias</h2>
                <div class="panel-group" id="accordion">
                    <%= render partial: "categories/catalog", collection: @categories, as: :c %>
            </div>
        </div>

_catalog Partial _catalog部分

<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">
                <%= c.name %>
                <span class="badge">4</span>
            </a>
        </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse in">
        <% c.subcategories.each do |sc| %>
            <div class="panel-body">
                <a href="#">
                    <%= sc.name %>
                </a>
            </div>
        <% end %>
    </div>
</div>

Controller 控制者

  def catalogo
    @categories = Category.where("parent_id IS NULL")
    unless params[:cat_id].blank?
      @products = Product.where(category_id: params[:cat_id]).paginate(:page => params[:page], :per_page => 50)
    else
      @products = Product.all.paginate(:page => params[:page], :per_page => 50)
    end
      @categorieswparent = Category.where("parent_id IS NOT NULL")
  end

Your IDs need to be unique. 您的ID必须是唯一的。 Instead of hard-coding id="collapse1" , use the ID of the record: 代替硬编码id="collapse1" ,使用记录的ID:

id="collapse<%= sc.id %>"

Perform the same interpolation for the href of the <a> tag responsible for collapsing and expanding the accordion. 对负责折叠和展开手风琴的<a>标签的href进行相同的插值。

Use category id id="#collapse<%= c.id %>" to interpolate with element id in _catalog Partial. 使用类别id id="#collapse<%= c.id %>"_catalog Partial中插入元素ID。

<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapse<%= c.id %>">
                <%= c.name %>
                <span class="badge">4</span>
            </a>
        </h4>
    </div>
    <div id="collapse<%= c.id %>" class="panel-collapse collapse in">
        <% c.subcategories.each do |sc| %>
            <div class="panel-body">
                <a href="#">
                    <%= sc.name %>
                </a>
            </div>
        <% end %>
    </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM