Rails - 嵌套表单不保存我的记录

时间:2016-09-19 15:13:13

标签: ruby-on-rails nested-attributes

用我的模特:

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>

它的作用如图所示:

business_day_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 - 使用爆炸从控制器中保存 我做的是添加一个&#34;!&#34;做我的控制器:

@lab = Lab.new(lab_params)
    if @lab.save!
....

结果是这个错误:

验证失败:必须存在工作日实验室

所以似乎没有保存实验室记录,这就是为什么不能创建工作日的权利,对吧?

2 个答案:

答案 0 :(得分:0)

你强大的参数不接受你日期的标识符。而不是:

def lab_params
  return params.require(:lab).permit(:name, :street, :city, :postal_code, :state, :country, business_days_attributes: [:day, :from_time, :to_time])
end

使用此:

def lab_params
  return params.require(:lab).permit(:name, :street, :city, :postal_code, :state, :country, business_days_attributes: [:business_day_id, :day, :from_time, :to_time])
end

def lab_params
  return params.require(:lab).permit(:name, :street, :city, :postal_code, :state, :country, business_days_attributes: [:id, :day, :from_time, :to_time])
end

答案 1 :(得分:0)

我明白了。 问题可能是实验室在工作日之前没有得到保存。

基于文档website我需要的是添加几行代码。现在一切正常!不管怎样,谢谢你!