【发布时间】:2016-09-20 00:31:21
【问题描述】:
使用我的模型:
class Lab < ApplicationRecord
has_many :business_days, dependent: :destroy
accepts_nested_attributes_for :business_days, reject_if: lambda {|attributes| attributes['kind'].blank?}
# ...
end
控制器:
def new
@lab = Lab
7.times { @lab.business_days.build}
end
我创建了一个表单来将我的实验室记录保存到数据库中。我想一次性节省可用工作日,所以我添加了以下内容:
<table class="table table-default">
<thead>
<tr>
<th></th>
<% @weekdays.each do |day| %>
<th class="text-center"><%= day[0..2].capitalize %></th>
<% end %>
</tr>
</thead>
<tbody>
<tr>
<th style="vertical-align: middle;">From:</th>
<% @lab.business_days.each.with_index do |bd, index| %>
<%= f.fields_for :business_days_attributes, index: index do |bd_form| %>
<%= bd_form.hidden_field :day, value: @weekdays[index] %>
<td><%= bd_form.text_field :from_time %></td>
<% end %>
<% end %>
</tr>
<tr>
<th style="vertical-align: middle;">To:</th>
<% @lab.business_days.each.with_index do |bd, index| %>
<%= f.fields_for :business_days_attributes, index: index do |bd_form| %>
<td><%= bd_form.text_field :to_time %></td>
<% end %>
<% end %>
</tr>
</tbody>
</table>
图片说明了它的作用:
我的创建操作如下所示:
def create
@lab = Lab.new(lab_params)
if @lab.save
end
lab_params 定义如下:
def lab_params
return params.require(:lab).permit(:name, :street, :city, :postal_code, :state, :country, business_days_attributes: [:day, :from_time, :to_time])
end
我的问题是当我保存尝试提交表单时,实验室记录被保存,但没有创建/保存工作日记录。
我的问题是 - 我在哪里做错了?
编辑 - 要求的参数:
Started POST "/labs" for ::1 at 2016-09-19 17:15:40 +0200
Processing by LabsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sbiXra6IQBpleuCqZ+zUFN+mDAzjSa/b9VgCYz6kL2VyeTkiqcldy5SOVXJCHr3HrWbUMCjtlBUrjXOBrWOhHA==", "lab"=>{"name"=>"Laboratory Uno", "street"=>"some street", "city"=>"some city", "business_days_attributes"=>{"0"=>{"day"=>"monday", "from_time"=>"00:00", "to_time"=>"15:00"}, "1"=>{"day"=>"tuesday", "from_time"=>"", "to_time"=>""}, "2"=>{"day"=>"wednesday", "from_time"=>"", "to_time"=>""}, "3"=>{"day"=>"thursday", "from_time"=>"", "to_time"=>""}, "4"=>{"day"=>"friday", "from_time"=>"", "to_time"=>""}, "5"=>{"day"=>"saturday", "from_time"=>"", "to_time"=>""}, "6"=>{"day"=>"sunday", "from_time"=>"", "to_time"=>""}}}, "commit"=>"Add to database"}
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.4ms) BEGIN
SQL (0.6ms) INSERT INTO "labs" ("name", "street", "city", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["name", "Laboratory Uno"], ["street", "some street"], ["city", "some city"], ["created_at", 2016-09-19 15:15:40 UTC], ["updated_at", 2016-09-19 15:15:40 UTC]]
(0.5ms) COMMIT
Redirected to http://localhost:3000/labs/7
Completed 302 Found in 17ms (ActiveRecord: 2.6ms)
编辑 2 - 删除 reject if
...params...
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.1ms) BEGIN
(0.8ms) ROLLBACK
...redirect
编辑 3 - 用 bang 从控制器保存 我所做的是添加一个“!”做我的控制器:
@lab = Lab.new(lab_params)
if @lab.save!
....
结果就是这个错误:
验证失败:必须存在工作日实验室
看来 Lab 记录没有保存,这就是无法创建工作日的原因,对吧?
【问题讨论】:
-
从终端显示错误日志
-
您能否从控制台包含发送到创建操作的参数?
-
包含参数。没有显示错误。
-
不是
kind总是空白吗?也许这就是阻止您保存记录的原因。 -
删除了拒绝 if 语句。包含编辑
标签: ruby-on-rails nested-attributes