代码是:
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
offers
WHEREoffers
.`` = 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| %>