Rails 打开同一个对象时出现模态问题

Problems with Modal in Rails opening the same object

我正在维护一个较旧的 Rails 3.2.22 应用程序,我正在尝试添加一个模式以弹出一个简单的表单来添加注释。

这是我的观点(为了便于阅读而删减)

<% @assigned.each do |call| %>
          <%= link_to "Notes", '#ajax-modal', data: { toggle: "modal", target: "#note-modal" }, class: 'btn btn-small btn-inverse' %>
        <%= render 'layouts/note_modal', :call => call %>
<% end %>

这是我的模态 app/views/layouts/_note_modal.html.erb

    <div id="note-modal" class="modal hide fade" tabindex="-1">
      <div class='modal-header'>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3 class="modal-title"><%= call.incident_number %> Notes</h3>
      </div>
      <div class='modal-body'>
        <div class="modal-body-content">       

  <%= form_for(@note, url: call_note_calls_path, html: {class: "form-inline"}) do |f| %>
                   <%= f.text_field :message, :maxlength => 255 %>
                   <%= f.hidden_field :call_id, value: call.id %>
                   <%= f.hidden_field :user_id, value: current_user.id %>
                  <%= f.button "Add Note", class: 'btn btn-info btn-small', data: {disable_with: "<i class='icon-spinner'></i>Posting..."} %>
                  <% end %></div>
    <div class="ajax-loader">
  </div>
  </div>
  <div class='modal-footer'>
    <button type="button" data-dismiss="modal" class="btn">Close</button>
  </div>
</div>

当我加载页面并有多个调用时,我单击注释 link,部分呈现在模态中,我可以提交注释。问题是如果我在屏幕上有 10 个呼叫,无论是哪个注释 link 我单击它都会为屏幕上的第一个呼叫添加一个注释。

我是否错误地传递了当地人?我知道这段代码在块内,所以它应该拉调用对象并将其作为局部传递给模态中的部分但它不是。

非常感谢任何帮助。

我认为问题在于您每次都为注释呈现模态,但它们都具有相同的 ID。所以你总是打开第一个模式。实际上恕我直言,您只需要渲染模态 一次 并通过触发模态的 link 上的数据属性传递 note_id 。所以基本上通过调用者配置模态。

我能够自己解决这个问题,并且仍然通过调用索引作为模态的目标 ID 的一部分在循环中渲染部分。下面的工作代码。

<% @assigned.each_with_index do |call, index| %>
  <%= link_to "Notes", '#note-modal', data: {toggle: "modal", target: "#note-modal#{index}" }, class: 'btn btn-small btn-inverse' %>
    <%= render 'layouts/note_modal', call: call, index: index %>
 <% end %>

<div id="note-modal<%= index %>" class="modal hide fade" tabindex="-1">
  <div class='modal-header'>
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3 class="modal-title"><%= call.incident_number %> Notes</h3>
  </div>
  <div class='modal-body'>
    <div class="modal-body-content">
      <%= form_for(@note, url: call_note_calls_path, html: {class: "form-inline"}) do |f| %>
               <%= f.text_field :message, :maxlength => 255 %>
               <%= f.hidden_field :call_id, value: call.id %>
               <%= f.hidden_field :user_id, value: current_user.id %>
              <%= f.button "Add Note", class: 'btn btn-info btn-small', data: {disable_with: "<i class='icon-spinner'></i>Posting..."} %>
              <% end %></div>
    <% call.notes.each do |n| %>
    <li><%= n.message %> added by <%= n.user.username %></li>
    <% end %>
    <div class="ajax-loader">
  </div>
  </div>
  <div class='modal-footer'>
    <button type="button" data-dismiss="modal" class="btn">Close</button>
  </div>
</div>