简体   繁体   English

Rails Bootstrap模态部分

[英]rails bootstrap modal partial

Using Rails 4.2.6, Bootstrap-saas 3.2.0 I have a collection of products which i'm rendering in my index view like this. 使用Rails 4.2.6,Bootstrap-saas 3.2.0,我有一些产品集,这些产品正像这样在索引视图中呈现。 Using bootstrap gridsystem 使用引导网格系统

.row
  =render partial: 'product', collection: @products

The product partial 产品偏

   .col-sm-6.col-md-4
    .thumbnail
    %h2 My Box
    %ul
      %li=product.name
      %li=product.brand
      %li=product.model
      %li="$#{product_price(product.price)}"
      =link_to 'more info', '#dialog', 'data-toggle' => 'modal'

I want to display a modal for each rendered product with product.description,product.name displayed in the modal. 我想为每个渲染的产品显示一个模态,并在模态中显示product.description,product.name。 The problem I'm having is, the modal is only loading with the first product information only. 我遇到的问题是,模态仅加载第一个产品信息。 It's supposed to show product information for each product. 它应该显示每个产品的产品信息。 my modal html markup 我的模态HTML标记

    #dialog.modal.fade{'aria-hidden' => 'true', 'aria-labelledby' =>         'myModalLabel', 'data-backdrop' => 'static', 'data-keyboard' => 'true', :role => 'dialog', :tabindex => '-1'}
  .modal-dialog
    .modal-content
      .modal-header
        %button.close{'aria-label' => 'Close', 'data-dismiss' => 'modal', :type => 'button'}
          %span{'aria-hidden' => 'true'} ×
        %h3.modal-title=product.name
      .modal-body
        %h2=product.desc
      .modal-footer
        =button_tag 'close', 'data-dismiss' => 'modal'

You are using a CSS id for your modal with '#dialog' selector. 您正在通过'#dialog'选择器为模态使用CSS id Your CSS id s must be unique on your page. 您的CSS id在您的页面上必须是唯一的。

So, you would have to add a unique identifier for your modal boxes. 因此,您将必须为模式框添加一个唯一的标识符。 For example adding a product.id to its id : 例如,将product.id添加到其id

=link_to 'more info', "#dialog-#{product.id}", 'data-toggle' => 'modal'

and in your modal partial: 并在您的模态部分中:

.modal.fade{id="dialog-#{product.id}", 'aria-hidden' => 'true', 'aria-labelledby' => 'myModalLabel', 'data-backdrop' => 'static', 'data-keyboard' => 'true', :role => 'dialog', :tabindex => '-1'}
  .modal-dialog
    .modal-content
      -# etc.

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

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