【问题标题】:Rails - nested form not saving my recordsRails - 嵌套表单不保存我的记录
【发布时间】: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


【解决方案1】:

您的强大参数不接受您的标识符。而不是这个:

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

【讨论】:

  • 我试图这样做,但仍然存在回滚并且没有保存记录
  • 你有任何验证吗?
  • 暂时没有。我把它们都关掉了。我想知道fields_for是否有办法将空数组作为索引传递,所以输入的名称看起来像:place[business_days_attributes][][from_time]?
  • 将 nil 传递给 index 创建了上述参数,但问题仍然存在。
【解决方案2】:

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

基于文档website,我只需要添加几行代码即可。现在一切正常!还是谢谢大家!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    相关资源
    最近更新 更多