Rails - 无意中使用同一对象的多种形式

时间:2014-02-07 16:57:18

标签: ruby-on-rails partial-views form-for

我有一个页面显示每个@user的详细信息。每个用户都有一个注册,可以有一个注释。当我例如这样做:

<% @user.each do |u| %>
  <% the_enrolment = Enrolment.where(user_id: u).first %>
<% end %>

然后,在每次迭代中,注册都是不同的注册,因为用户在每次迭代中都会更改。到目前为止,这么简单。

但是当我将这个注册变量(循环遍历)传递给包含表单的部分时,如下所示:

<%= render 'the_partial', enrolment: the_enrolment %>

使用此表单(在部分视图中):

<%= form_for enrolment, :url => the_path, remote: true do |f| %>
  <%= f.hidden_field :id %>
  <%= f.text_area :comment %>
<%= f.submit "Save" %>

然后,在最终呈现的页面中,每个表单使用相同的注册。 (它们都具有相同的ID,每个text_are都预先填充了一个的内容。我在这里做错了什么?

完整的迭代看起来像这样:

@user.each do |u|
  the_enrolment = Enrolment.where(user_id: u).first
  <%= the_enrolment %> # correct id
  <%= render 'the_partial', enrolment: the_enrolment %>
end

非常感谢!

更新

这是一个包含最新信息的更新。问题似乎是表单放在twitter bootstrap模式中。这是整个partial

<%= enrolment.id %> # here it's the correct id
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Admin Comment</h3>
  </div>

  <%= form_for enrolment, :url => the_path, remote: true do |f| %>

      <div class="modal-body">
        <p>
          <%= "enrolment in modal: #{enrolment.id}" %> # at this place the id is ALWAYS the id of the very first enrolment
          <%= f.hidden_field :id %>
          <%= f.text_area :comment_admin %>

          <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
          <%= f.submit "Save", class: "btn btn-success", id: "admin-comment-submit-button" %>
        </p>
      </div>
  <% end %>
</div>

如果我删除此页面上有关模态的所有内容,则表单有效。

0 个答案:

没有答案