在.each循环中显示一个条目的模态 - Rails/Bootstrap

bvc*_*vcm 3 jquery ruby-on-rails twitter-bootstrap

我想要一个链接来打开一个显示单击对象(word.title)的模态,显示在每个循环中.现在它打开模态,然后再次为循环中的每个项目显示它.

    <h1>Glossary of words</h1>
    <p>Pagination at 25</p>

    <table class="table table-hover">
        <thead>
            <tr>
                <th>Title</th>
                <th>Definition</th>
                <th>Usage</th>
                <th>Word Type</th>
            </tr>
        </thead>
        <tbody>
        <% @words.each do |word| %>
            <tr>
                <th scope="row">
                    <a href="#" data-toggle="modal" data-target=".bs-example-modal-sm">
                        <%= word.title %>
                    </a>
                        <div class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
                          <div class="modal-dialog modal-sm">
                            <div class="modal-content">
                              <%= word.title %>
                            </div>
                          </div>
                        </div>
                </th>
                <td><%= word.title %></td>
                <td><%= word.definition %></td>
                <td><%= word.word_type %></td>
            </tr>

        <% end %>
        </tbody>
    </table>

// Word Modal
$('.bs-example-modal-sm').modal()
Run Code Online (Sandbox Code Playgroud)

min*_*noz 8

要使链接调用正确的模式,您需要分配id给每个模式modals.并使用data-target属性通过传入来调用模态id of modal.

你的代码可能看起来像这样.

<a href="#" data-toggle="modal" data-target="#modal-<%= word.id %>">
    <%= word.title %>
</a>

<div id="modal-<%= word.id %>" class="modal fade bs-example-modal-sm" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <%= word.title %>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

来源:http://getbootstrap.com/javascript/#live-demo