更新模型,Child删除孙子:( Rails

时间:2018-11-01 15:10:02

标签: ruby-on-rails nested

我有:

Initial_application.rb

has_one :full_application
has_one :statement, through: :full_application
accepts_nested_attributes_for :application_status
accepts_nested_attributes_for :full_application

Full_application.rb

has_one :statement
has_one :plaid_auth, through: :statement

Statement.rb

has_many_attached :files
belongs_to :full_application, optional: true

所以我有 -initial_application -full_application(initial_application的子代) -语句(full_application的子代,因此是initial_application的子代)

initial_application_controller.rb

def edit_full
  respond_to do |format|
  format.html
  format.js
  render layout: 'fullapplication_layout'
  end
end

我在控制器中创建了一个操作,以更新初始应用程序和full_application。创建了一个嵌套表格等,并放置了强参数等。

def initial_application_params
  params.require(:initial_application).permit( :source, :amount, :years, :sales, :creditscore, :email, :name, :phone, :interest, :businessname, application_status_attributes: [:status, :person, :status_name, :status_description, :status_note], full_application_attributes: [:amount, :years, :sales, :creditscore, :email, :phone, :name, :defaults, :existingach, :acceptcc, :seasonal, :balance, :lender, :interest, :street, :apt, :city, :state, :zipcode, :businessname, :bstreet, :bapt, :bcity, :bstate, :bzipcode, :dba, :legalentity, :fedtaxid, :dob, :percentage, :own, :bown, :bphone, :analytics, :source, :credit_auth, :ip ] )
end

edit_full_form.erb

<%= simple_form_for @initial_application do |f| %>
...
<%= f.simple_fields_for :application_status do |p| %>
...
<% end %> 
<%= f.simple_fields_for :full_application do |m| %>
...
<% end %>
<%= f.button :submit, "Update Status", class: "button is-medium" %>
<% end %>

但是,当我使用嵌套表单更新initial_application和full_application时,它将删除语句。我发现了问题,但不知道如何解决。

“更新”操作正在创建新的full_application记录,而不更新旧的full_application。

1 个答案:

答案 0 :(得分:0)

由于未通过其id,因此正在重新创建您的full_aplication记录。正如documentation所说:

  

对于每个没有的哈希具有id键,除非该哈希还包含一个评估为{{1}的_destroy键,否则将实例化一条新记录。 }。

要解决此问题,您需要在允许的参数true的嵌套列表中添加:id,并将该ID作为hidden_​​field传递给表单。