0

代码是:

class Client < ActiveRecord::Base
  ...
  has_one :offer, :dependent => :destroy
  accepts_nested_attributes_for :offer
  ...
end

class Offer < ActiveRecord::Base
  ...
  belongs_to :client
  ...
end


<%= simple_form_for [:admin, @client], :html=>  {:multipart => true } do |f| %>
..some input fields
<%= f.simple_fields_for :offer, @client.new_record? ? Offer.new : ""  do |o| %>
..some input fields

这适用于新条目,但不会更新。

错误:Mysql2::Error: Unknown column 'offers.' 在“where 子句”中:DELETE FROM offersWHERE offers.`` = 718

当我将客户端模型更改为:

accepts_nested_attributes_for :offer, :reject_if => lambda {|a| a[:name].blank?}

错误消息消失了,它更新了客户表,但不更新报价表。

这是有效的:

  has_one :offer, :dependent => :destroy
  accepts_nested_attributes_for :offer, allow_destroy: true

  belongs_to :client

  <%= f.simple_fields_for :offer, @client.new_record? ? Offer.new : @client.offer  do |o| %>
4

1 回答 1

1

首先,它不适用于更新似乎是合乎逻辑的,就像你正在做的那样:

<%= f.simple_fields_for :offer, @client.new_record? ? Offer.new : ""  do |o| %>

的简单字段将尝试为“”创建字段,而它应该使用 offer 对象。你可以用(可能有更好的方法来解决这个问题,这只是一个解决方案,而不是解决方案)

<%= f.simple_fields_for :offer, @client.offer.new_record? ? Offer.new : @client.offer  do |o| %>

编辑:我认为最好只在不存在的情况下构建报价,所以它看起来像这样:

<% @client.build_offer if @client.offer.new_record? %>
<%= f.simple_fields_for :offer do |o| %>
于 2012-10-17T20:38:27.750 回答