Jquery 将表行中的实例变量传递给modal

Jquery 将表行中的实例变量传递给modal,jquery,ruby-on-rails,twitter-bootstrap,Jquery,Ruby On Rails,Twitter Bootstrap,在我的应用程序中,我有一个表格,其中显示了用户发布的帖子列表。在每一行中,都有一个按钮打开一个模式,显示已发送消息响应该帖子的用户列表。我无法将正确的变量传递到模式 如果我运行下面的@conversations循环,我用它来获取用户列表,在模式之外和行本身,它会工作。但是当我尝试在模式中使用循环时,它使用第一行的offer,而不是单击按钮所在行的offer 例如,如果我有一个包含以下标题的报价列表: 这是第一个报价 这是第二个报价 这是第三个报价 在模式中运行的命令,将使用第一个报价。tit

在我的应用程序中,我有一个表格,其中显示了用户发布的帖子列表。在每一行中,都有一个按钮打开一个模式,显示已发送消息响应该帖子的用户列表。我无法将正确的变量传递到模式

如果我运行下面的
@conversations
循环,我用它来获取用户列表,在模式之外和行本身,它会工作。但是当我尝试在模式中使用循环时,它使用第一行的
offer
,而不是单击按钮所在行的
offer

例如,如果我有一个包含以下标题的报价列表:

  • 这是第一个报价
  • 这是第二个报价
  • 这是第三个报价
在模式中运行的命令
,将使用第一个
报价。title
,“这是第一个报价”。即使我单击第三行中的按钮。因此,该模式仅适用于第一行,然后在从任何其他行打开时显示相同的用户

<% @offers.each do |offer| %>
        <tr>



          <td><%= link_to offer.title, offer_path(offer) %></td>


            <%= button_to offer_path(offer),
                          :id => 'contacts', "data-toggle" => "modal", 'data-target' => '#showContactsModal',
                          :class => 'btn btn-success', :style => "border-radius: 0;" do %>
                <i class="glyphicon glyphicon-ok"></i> View Users Who Contacted You
            <% end %>





            <div class="modal fade" id="showContactsModal">
              <div class="modal-dialog">
                <div class="modal-content">
                  <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">
                      <span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                    <h4 class="modal-title">
                      Who contacted you about this offer?</h4>
                  </div>
                  <div class="modal-body">

                    <% @conversations.each do |reply| %>
                        <% if reply.subject == offer.title %>
                            <%= reply.originator.name %>
                        <% end %>
                    <% end %>

                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Submit</button>
                  </div>
                </div>
                <!-- /.modal-content -->
              </div>
              <!-- /.modal-dialog -->
            </div>
            <!-- /.modal -->
          </td>
      </td>

   </tr>

<% end %>

“联系人”、“数据切换”=>“模式”、“数据目标”=>“#显示联系人模式”,
:class=>'btn btn success',:style=>“边界半径:0;”do%>
查看与您联系的用户
&时代;接近
谁联系过你这个提议?
接近
提交
我如何解决这个问题,以便模态为其行接收正确的变量

我刚刚注意到,在我的
按钮中,我提供了offer show视图的路径,这是不必要的,我只希望它显示模式。我应该给它一个零路径还是什么


请随意编辑此问题,以使其更为一般。

您的模式将在具有相同ID的报价循环中多次创建(
showContactsModal
)。您创建的按钮只激活第一个模式,因为它正在调用该ID

因此,您的
按钮中的
数据目标
需要设置为一些唯一的值,例如:

<%= button_to offer_path(offer),
    :id => 'contacts', "data-toggle" => "modal", 
    'data-target' => '#showContactsModal_' + offer.id.to_s,
    :class => 'btn btn-success', :style => "border-radius: 0;" do %>
        <i class="glyphicon glyphicon-ok"></i> View Users Who Contacted You
<% end %>

这样,JS就知道向用户显示哪个模式

部分问题可能是由于代码中的某些格式不正确造成的。除此之外,我想到的下一件事是,由于您没有为每行中的每个模式使用唯一的ID(我相信您希望每个行都有一个模式),因此您没有正确地替换内容

<% @offers.each do |offer| %>
  <tr>
    <td>
      <%= link_to offer.title, offer_path(offer) %>
    </td>
    <td>
      <%= button_to offer_path(offer), :id => 'contacts', "data-toggle" => "modal", 'data-target' => '#showContactsModal_#{offer.id}', :class => 'btn btn-success', :style => "border-radius: 0;"%>
      <i class="glyphicon glyphicon-ok">View Users Who Contacted You</i>
    </td>
    <td>
      <div class="modal fade hide" id="showContactsModal_#{offer.id}">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">
                <span aria-hidden="true">&times;</span>
                <span class="sr-only">Close</span>
              </button>
              <h4 class="modal-title">Who contacted you about this offer?</h4>
            </div>
            <div class="modal-body">
              <% @conversations.each do |reply| %>
                <% if reply.subject == offer.title %>
                  <%= reply.originator.name %>
                <% end %>
              <% end %>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Submit</button>
            </div>
          </div>
        </div>
      </div>
   </td>
  </tr>

<% end %>

'contacts','data toggle'=>'modal','data target'=>'#showContactsModal#{offer.id}',:class=>'btn btn success',:style=>“边框半径:0;”%>
查看与您联系的用户
&时代;
接近
谁联系过你这个提议?
接近
提交

完美,现在工作正常。谢谢你明确的回答。
<% @offers.each do |offer| %>
  <tr>
    <td>
      <%= link_to offer.title, offer_path(offer) %>
    </td>
    <td>
      <%= button_to offer_path(offer), :id => 'contacts', "data-toggle" => "modal", 'data-target' => '#showContactsModal_#{offer.id}', :class => 'btn btn-success', :style => "border-radius: 0;"%>
      <i class="glyphicon glyphicon-ok">View Users Who Contacted You</i>
    </td>
    <td>
      <div class="modal fade hide" id="showContactsModal_#{offer.id}">
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">
                <span aria-hidden="true">&times;</span>
                <span class="sr-only">Close</span>
              </button>
              <h4 class="modal-title">Who contacted you about this offer?</h4>
            </div>
            <div class="modal-body">
              <% @conversations.each do |reply| %>
                <% if reply.subject == offer.title %>
                  <%= reply.originator.name %>
                <% end %>
              <% end %>
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Submit</button>
            </div>
          </div>
        </div>
      </div>
   </td>
  </tr>

<% end %>