如何从for循环将变量传递给模态

时间:2016-12-03 15:09:08

标签: ruby-on-rails ruby

我有for循环,我在循环的每次迭代中调用partial:

<% applications.each do |application| %>
   <td>
     <%= render :partial => "layouts/form", :locals => {:@application => application} %>
   </td>
<% end %>

现在,在部分内部,我有一个按钮,按下时会显示一个模态:

<button type="button" class="button-custom btn-primary" data-toggle="modal" data-target="#form-modal"> <%= "#{button_text}" %> </button>
<div id="form-modal" class="modal fade modal-form" role="dialog" tabindex='-1'>
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-body">
        <td> <%= "#{@application.name}" %> </td>

现在的问题是@application.name总是引用for循环中第一个应用程序的值。它没有得到与循环的相应迭代相对应的值。

1 个答案:

答案 0 :(得分:1)

@变量中删除application符号。

 <%= render :partial => "layouts/form", :locals => {:application => application} %>
 ....

<td> <%= application.name %> </td>

您应该为每个应用程序生成唯一的模态ID。

data-target="#form-modal-<%= application.id %>"

<div id="form-modal-<%= application.id %>"