在RubyonRails中自动填充嵌套表单字段的第一个实例

在RubyonRails中自动填充嵌套表单字段的第一个实例,ruby,ruby-on-rails-5,nested-forms,Ruby,Ruby On Rails 5,Nested Forms,使用Ruby 2.3.3和Rails 5.1.4 我有一个供用户填写的表单,它使用嵌套表单字段Gem允许用户添加动态数量的组成员。我希望在加载表单时,第一个实例已经填写并填充为current_user.email 这是我的班级 class Group < ApplicationRecord has_many :teammates, dependent: :destroy accepts_nested_attributes_for :teammates, allow_destroy:

使用Ruby 2.3.3和Rails 5.1.4

我有一个供用户填写的表单,它使用嵌套表单字段Gem允许用户添加动态数量的组成员。我希望在加载表单时,第一个实例已经填写并填充为current_user.email

这是我的班级

class Group < ApplicationRecord
  has_many :teammates, dependent: :destroy
  accepts_nested_attributes_for :teammates, allow_destroy: true
end
类组
这是我的表格

<%= form_with(model: group, local: true) do |form| %>
<div class="form-group row">
  <%= form.nested_fields_for :teammates do |member| %>
      <div align="right" class="col-md-3">
       <label>Group Member: </label>
      </div>
      <div class="col-md-6">
        <%= member.text_field :email, placeholder: 'email', class: 'input' %>
        <%= member.remove_nested_fields_link %> <br />
      </div>
  <%end %>
  <%= form.add_nested_fields_link :teammates, '+ Add a Group Member', id: 'addGroupMember' %>
</div>

组员:


通常是这样做的

class GroupsController
  def edit
    @group.teammates.build(email: current_user.email)
  end
end
您只构建了一个队友对象,它还没有被保存。但它足以让形体看到它并相应地呈现自己