嵌套字段的值未正确保存 | Rails 5.2

Values for nested fields not saving without error | Rails 5.2

我正在开发一个 Rails 5.2 应用程序,需要存储一个 Quote 和一个 QuoteLineItem 来使用它(Quote has_one :quote_line_item, QuoteLineItem belongs_to :quote).我已经根据文档进行了所有设置,但是当提交 quotes#edit for(其中包含 fields_for @quote.quote_line_item...)时,Quote 记录的值在数据库中更新,但是值QuoteLineItem 不是。提交时没有出现错误,服务器日志中也没有 "Unpermitted params..." 消息。

报价模型

class Quote < ApplicationRecord
  has_one :quote_line_item, inverse_of: :quote, dependent: :destroy
  accepts_nested_attributes_for :quote_line_item, update_only: true
end

QuoteLineItem 模型

class QuoteLineItem < ApplicationRecord
  belongs_to :quote, inverse_of: :quote_line_item, touch: true
end

报价控制器

class QuotesController < ApplicationController
  def update
    @quote = Quote.find(params[:id])

    if @quote.update(quote_params)
      flash[:success] = "Quote was successfully updated."
      redirect_to @quote
    else
      flash[:error] = "Quote was not updated. Please try again."
      render :edit
    end
  end

  private
    def quote_params
      params.require(:quote).permit(:issued_at, quote_line_item_attributes: [ :kind, :description, :price ])
    end
end

引用编辑视图

<%= form_for @quote do |quote_form| %>
  <% if @quote.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@quote.errors.count, "error") %> prohibited this quote from being saved:</h2>

      <ul>
        <% @quote.errors.full_messages.each do |message| %>
          <li><%= message %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <%= quote_form.label :issued_at %>
  <%= quote_form.datetime_field :issued_at %>

  <%= fields_for :quote_line_item, @quote.quote_line_item do |quote_line_item_fields| %>
    <%= quote_line_item_fields.label :description %><br>
    <%= quote_line_item_fields.text_field :description %>

    <%= quote_line_item_fields.label :price %>
    <%= quote_line_item_fields.number_field :price, step: :any %>
  <% end %>
<% end %>

服务器日志

Started PATCH "/quotes/62" for 127.0.0.1 at 2018-11-12 13:01:46 +0800
Processing by QuotesController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"bVa+vhRayuPDhe5xkLcK2rm10zQ9oHvtDSZsKDhMBsdX/JDjf6uXsnIJ7gM/yP7Lt9E+aBGIR9WCoLU2uNhgVQ==", "quote"=>{"issued_at"=>"12/11/2018 11:05 AM"}, "quote_line_item"=>{"description"=>"QuoteLineItem description goes here...", "price"=>"800.00"}, "commit"=>"Save", "id"=>"62"}
  Quote Load (3.8ms)  SELECT  `quotes`.* FROM `quotes` WHERE `quotes`.`id` = 62 LIMIT 1
  ↳ app/controllers/quotes_controller.rb:127
   (0.7ms)  BEGIN
  ↳ app/controllers/quotes_controller.rb:53
   (3.6ms)  COMMIT
  ↳ app/controllers/quotes_controller.rb:53
Redirected to http://localhost:3000/quotes/62
Completed 302 Found in 44ms (ActiveRecord: 9.3ms)

为什么这不应该起作用?

谢谢。

尝试在 fields_for:

之前引用 quote_form
<%= form_for @quote do |quote_form| %>

  ...

  <%= quote_form.fields_for :quote_line_item do |quote_line_item_fields| %>
  #   ^ <- Add here

    ...

  <% end %>
<% end %>

更新后,检查传递给 quotes#edit 的参数是否嵌套:

即:"quote"=>{"issued_at"=>"12/11/2018 11:05 AM", "quote_line_item"=>{"description"=>"Line item description goes here...", "price"=>"200.00"}, "id"=>"63"}