序列化的fields_for在编辑时不从db填充

时间:2016-06-14 13:58:32

标签: ruby-on-rails forms ruby-on-rails-4 populate fields-for

在此表单的编辑页面上,fields_for标记之外的所有字段(收件箱名称,自动协调以及此处未列出的其他一些字段)都将根据其相应的db值填充。但是,fields_for标记内的所有内容都没有,即使它们正好发布到数据库中也不错。

我发布了:group_member_roles作为示例,但是他们自己的其他fields_for中还有一些其他字段正在做同样的事情。它只会令人困惑,它会发布到数据库但不会在编辑时显示。

我读到fields_for的次数越多,我觉得我没有正确使用它。似乎更倾向于在您的表单当前引用的表之外填充数据库表,但我只是尝试序列化收件箱表中的数据。当我查看:group_member_roles列时,我希望它是一个包含进程true / false,action add / delete和一串值的数组/哈希。

#_form.html.erb

<%= form_for(@inbox) do |f| %>
    <%= f.label :inbox_name %>
    <%= f.text_field :name, placeholder: "Inbox Name" %>
  <%= f.label :automatic_reconciliation, "Turn on/off automatic reconciliation" %>
  <div class="switch small">
    <%= f.check_box :automatic_reconciliation, class: "switch-input" %>
    <label class="switch-paddle" for="inbox_automatic_reconciliation">
      <span class="show-for-sr">Automatic reconciliation</span>
      <span class="switch-active" aria-hidden="true">On</span>
      <span class="switch-inactive" aria-hidden="true">Off</span>
    </label>
  </div>
  <%= f.fields_for :group_member_roles do |group_member_roles| %>
  <h4>Group Member Roles</h4>
  <%= group_member_roles.label :process, "Turn On/Off Processing" %>
  <div class="switch small">
    <%= group_member_roles.check_box :process, class: "switch-input" %>
    <label class="switch-paddle" for="inbox_group_member_roles_process">
      <span class="show-for-sr">Group Member Roles Processing</span>
      <span class="switch-active" aria-hidden="true">On</span>
      <span class="switch-inactive" aria-hidden="true">Off</span>
    </label>
  </div>

  <%= group_member_roles.label :action, class: "hide" %>
  <%= group_member_roles.select :action, ["Add", "Delete"], { selected: "Add" }, { class: "hide" } %>

  <%= group_member_roles.label :values %>
  <%= group_member_roles.text_field :values, placeholder: "1234, 1337, 1986" %>
  <% end %>

提前感谢您的任何帮助或指导。

1 个答案:

答案 0 :(得分:0)

字段存储为散列,字段正在寻找要填充的对象,因此我在fields_for中添加了一个OpenStruct虚拟对象来实现它。如果有人能想出更好的方法,请告诉我,因为这是非常难看的代码。

  <%= f.fields_for :group_member_roles, OpenStruct.new(f.object.group_member_roles) do |group_member_roles| %>