在Rails 4中使用单个表单保存多个记录

时间:2014-05-15 08:53:27

标签: ruby-on-rails ruby-on-rails-4

我有一个名为家庭的模型,其中属于用户 我想让用户在单个表单中添加多个家庭成员,这位于/views/families/new.html.erb

编辑:抱歉,我之前应该提供更详细的代码 经过代码中的轻微修改后

在routes.rb中     资源:家庭 编辑:在 User.rb

class User < ActiveRecord::Base
  has_many :families
end

编辑:在 Family.rb

class Family < ActiveRecord::Base
belongs_to :user
end

在FamiliesController中

def new

end
编辑:我用这种方式修改了表单:

<%= form_tag(controller: "families") do %>
<% i = 0 %>
<% 3.times do %>
<br>
<h2>Family Member #<%= i+1 %> : </h2>
<br>
<%= fields_for("family[#{i}]") do |f| %>
    <%= f.text_field :name, class: "form-control" , placeholder: "Name"%> <br>
    <%= f.text_field :relationship, class: "form-control" , placeholder: "Relationship" %> <br>
    <%= f.date_field :dob, class: "form-control" %> <br>
    <%= f.text_field :blood_group, class: "form-control", placeholder: "Blood group" %> <br>
    <%= f.text_field :email, class: "form-control", placeholder: "Email" %> <br>
    <%= f.number_field :phone, class: "form-control", placeholder: "Phone number" %> <br>
    <%= f.text_area :hobbies, class: "form-control", placeholder: "Hobbies" %> <br>
    <% i=i+1 %>
<% end %>
<hr>
<% end %> <br>
<%= submit_tag "Submit", class: "btn btn-primary" %> <br>

编辑:在FamiliesController中创建动作

def create

        @family = Family.new(family_params)
        a = params[:family][:"0"]
        @family.user_id = current_user.id
        if @family.save
        flash[:notice] = params[:family][:"0"]

    redirect_to dummy_path
else
    flash[:notice] = "didnt happen"
end
end
  • 因此,用户会看到一个表格,用于为一个user_id
  • 创建3个家庭成员
  • 将数据输入3个相同的表单,然后点击提交
  • 来自 3个单独表单的数据应仅在提交一次时保存为数据库中的3个不同记录
编辑:我正在使用强大的参数,如下:

def family_params
    params.require(:family).permit(:all)
end
  • 现在,当我提交表单时,db中的所有值都保存为NULL。

  • 我做错了什么?我不应该使用强力的参数吗?

4 个答案:

答案 0 :(得分:8)

您想使用nested forms。您应该能够复制并粘贴以下代码以开始使用:

应用/模型/ family.rb

class Family < ActiveRecord::Base
  has_many :users

  accepts_nested_attributes_for :users
end

应用/模型/ user.rb

class User < ActiveRecord::Base
  belongs_to :family
end

应用/视图/家庭/ new.html.erb

<%= form_for(@family) do |f| %>
  <%= f.fields_for :users do |user| %>
    <p>
    <%= user.text_field :name %><br>
    <%= user.text_field :email %>
    </p>
  <% end %>

  <%= f.submit %>
<% end %>

应用/控制器/ families_controller.rb

  [...]

  def new
    @family = Family.new
    3.times { @family.users.build }
  end

  [...]

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_family
      @family = Family.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def family_params
      params.require(:family).permit(:name, users_attributes: [ :name, :email ])
    end

答案 1 :(得分:3)

我总是这样做的方法是制作一个新动作update_multiple。这需要具有以下结构的params(使用您的Family示例,并使用ID 123和456更新两个记录)

params = {:families => {123 => {:name => "foo", :address => "bar"}, 456 => {:name => "baz", :address => "qux"} }}

并且动作如下:

@families = []
params[:families].each do |id, attributes|
  if family = Family.find_by_id(id)
    family.update_attributes(attributes)
    @families << family
  end
end

为了设置具有所需结构的参数,您的表单将如下设置:

<% @families.each do |family| %>
  <div class="family">
    <label>Name: <%= text_field_tag "families[#{family_id}][name]", family.name %></label>
    <label>Address: <%= text_field_tag "families[#{family_id}][address]", family.address %></label>
  </div>
<% end %>

如果它获得params [:family],则可以改变update动作的结构来改变你的{{1}}动作的结构,如果它得到params [:families],则改变上面的代码。

答案 2 :(得分:1)

您需要像这样调整代码。假设您在users表中有属性name

在您的用户模型中

class User < ActiveRecord::Base
    has_many :families, :dependent => :destroy 
    accepts_nested_attributes_for :families, :allow_destroy => true
end

在您的users_controller

def new
@user = User.new
@family = 3.times { @user.families.build } #here
end

您的强参数应该如下所示

def user_params 
params.require(:user).permit(:name, families_attributes: [:name,:email]) 
end

#users/new.html.erb
<%= form_for(@user) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<%= f.fields_for :familes do |builder| %>
<%= render 'family_fields', :f => builder %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

#users/_family_fields.html.erb
<div>
<%= f.label :name, "Name" %>        
<%= f.text_field :name %>
<br>
<%= f.label :email, "Email" %>
<%= f.text_field :email %>
</div>

答案 3 :(得分:0)

我更喜欢使用 gem systemctl restart dockerSlimSimple form 进行表单操作。使用 cocoon,您可以Cocoon 任意数量的子记录。

用户模型

add/remove

控制器

class User < ActiveRecord::Base
    has_many :families, dependent: :destroy 
    accepts_nested_attributes_for :families, reject_if: :all_blank, allow_destroy: true
end

表格

class UsersController < ApplicationController
  def new
    @user = User.new
    @user.families.build # This builds default nested form in cocoon
  end

  def create
    @user = User.new(user_params)
    @user.save
  end

  private
  def user_params
    params.require(:user).permit(
      :name, :email,
      families_attributes: [:id, :name, :relationship, :_destroy])
  end
end

茧部分.form = simple_form_for(@user) do |f| = f.input :name, label: false, input_html: { placeholder: 'Name' } = f.input :email, label: false, input_html: { placeholder: 'Email' } .cocoon-form = f.simple_fields_for :families do |family| = render 'family_fields', f: family .links = link_to_add_association 'Add a member', f, :families .form-actions = f.submit

_family_fields.html.slim

快乐编码! :-)