隨着我的模型:導軌 - 嵌套形式不保存我的記錄
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
我的問題是當我保存嘗試提交表單時,Lab記錄被保存但沒有創建/保存BusinessDay記錄。
我的問題是 - 我在哪裏犯了一個錯誤?
編輯 - 參數數量的要求:
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 - 從控制器節能與爆炸 我所做的一切是添加一個「!」做我的控制器:
@lab = Lab.new(lab_params)
if @lab.save!
....
,結果是這個錯誤:
驗證失敗:工作日實驗室必須存在
如此看來,實驗室記錄不會被保存,這就是爲什麼工作日不能創建,對吧?
顯示來自終端的錯誤日誌 – luissimo
您能否從控制檯中包含發送到創建操作的參數?包含的參數包括 –
。沒有錯誤顯示。 – Ancinek