我正在尝试将多个具有相同名称的字段保存到数据库记录
中这就是我现在所拥有的:
<%= form_for @complaint, url: {action: 'create'}, :html => {:multipart => true} do |f| %>
<%= f.label :complaint_info, 'Describa lo que sucedió' %>
<%= f.cktext_area :complaint_info, :class => 'someclass', :ckeditor => {:language => 'us', :toolbar => 'mini'}, tabindex: '-1' %>
<%= f.fields_for :witnesses do |witnesses_form| %>
<%= witnesses_form.label :name, '¿Hay testigos?' %>
<%= witnesses_form.text_field :name, placeholder: 'Escriba el nombre del testigo', tabindex: '-1' %>
<%= witnesses_form.label :phone, 'Número de teléfono del testigo' %>
<%= witnesses_form.text_field :phone, placeholder: 'Escriba el número de teléfono' %>
<%= witnesses_form.label :name, '¿Hay testigos?' %>
<%= witnesses_form.text_field :name, placeholder: 'Escriba el nombre del testigo', tabindex: '-1' %>
<%= witnesses_form.label :phone, 'Número de teléfono del testigo' %>
<%= witnesses_form.text_field :phone, placeholder: 'Escriba el número de teléfono' %>
<% end %>
<% end %>
在控制器中:
def new
@complaint = Complaint.new
@complaint.witnesses.build
end
def create
@complaint = current_user.complaints.build(complaint_params)
if @complaint.save
redirect_to dashboard_complaint_path(@complaint)
else
render 'new'
end
end
private
def complaint_params
params.require(:complaint).permit(:complaint_info, witnesses_attributes: [:name, :phone])
end
我想要做的是为用户提供添加多个证人的选项,并将每个证人保存到数据库中的新记录中。
编辑:
以下是模型:
诉:
class Complaint < ActiveRecord::Base
belongs_to :user
has_many :witnesses
accepts_nested_attributes_for :witnesses
end
和见证人:
class Witness < ActiveRecord::Base
belongs_to :complaint
end
EDIT2:
控制器返回的内容
Started POST "/dashboard/complaints" for 127.0.0.1 at 2015-06-10 12:14:10 -0400
Processing by Dashboard::ComplaintsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"0ek0KaaYdUR1ktwUOLCeEdAG0fB63WOIJIIXL7ASM1zMbXWDHyCcXce82ijpLJRmneHs39Um9EehL/GWJkPqiw==", "complaint"=>{"complaint_info"=>"<p>This is the description</p>\r\n", "witnesses_attributes"=>{"0"=>{"name"=>"Second witness", "phone"=>"7877877877"}}}, "button"=>""}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.3ms) BEGIN
SQL (0.8ms) INSERT INTO "complaints" ("complaint_info", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["complaint_info", "<p>This is the description</p>\r\n"], ["user_id", 1], ["created_at", "2015-06-10 16:14:10.452890"], ["updated_at", "2015-06-10 16:14:10.452890"]]
SQL (1.3ms) INSERT INTO "witnesses" ("name", "phone", "complaint_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "Second witness"], ["phone", "7877877877"], ["complaint_id", 19], ["created_at", "2015-06-10 16:14:10.457497"], ["updated_at", "2015-06-10 16:14:10.457497"]]
(96.3ms) COMMIT
Redirected to http://localhost:3000/dashboard/complaints/19
Completed 302 Found in 125ms (ActiveRecord: 98.9ms)
如您所见,它只保存一个见证记录["name", "Second witness"]
,并且是最后一个字段(有两个字段,我在最后一个字段中输入了First witness
和Second witness
。)< / p>
答案 0 :(得分:1)
嗯,这是方法。假设你想要2条记录 save 。然后你需要做:
def new
@complaint = Complaint.new
2.times { @complaint.witnesses.build }
end
将表单更改为:
<%= form_for @complaint, url: {action: 'create'}, :html => {:multipart => true} do |f| %>
<%= f.label :complaint_info, 'Describa lo que sucedió' %>
<%= f.cktext_area :complaint_info, :class => 'someclass', :ckeditor => {:language => 'us', :toolbar => 'mini'}, tabindex: '-1' %>
<%= f.fields_for :witnesses do |witnesses_form| %>
<%= witnesses_form.label :name, '¿Hay testigos?' %>
<%= witnesses_form.text_field :name, placeholder: 'Escriba el nombre del testigo', tabindex: '-1' %>
<%= witnesses_form.label :phone, 'Número de teléfono del testigo' %>
<%= witnesses_form.text_field :phone, placeholder: 'Escriba el número de teléfono' %>
<% end %>
<% end %>
对于与fields_for
对象关联的每个witnesses
实例,将重复嵌套f
调用的