对象需要,得到ActiveSupport :: HashWithIndifferentAccess嵌套表单

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象需要,得到ActiveSupport :: HashWithIndifferentAccess嵌套表单相关的知识,希望对你有一定的参考价值。

我得到这个错误以前没见过,在这里尝试了所有类似问题的解决方案,我试图选择一个租户住在一个预先填充的议会模型的委员会,使用通过议会历史,表格显示然而,当我按提交时,我不断收到此错误。感谢这个问题的任何帮助。

Started PUT "/properties/6/build/council" for 127.0.0.1 at 2013-08-18 22:32:55 +0100
Processing by Properties::BuildController#update as html
  Parameters: {"utf8"=>"✓",    "authenticity_token"=>"wBWQaxtBioqzGLkhUrstqS+cFD/xvEutXnJ0jWNtSa0=", "property"=>   {"council_history"=>{"id"=>"1"}}, "commit"=>"Save Property[council history]",  "property_id"=>"6", "id"=>"council"}
  Property Load (0.3ms)  SELECT "properties".* FROM "properties" WHERE "properties"."id" =   ? LIMIT 1  [["id", "6"]]
   (0.1ms)  begin transaction
   (  0.1ms)  rollback transaction
 Completed 500 Internal Server Error in 62ms
 ActiveRecord::AssociationTypeMismatch - CouncilHistory(#70308444526760) expected, got          ActiveSupport::HashWithIndifferentAccess(#70308443935640):
activerecord (3.2.13) lib/active_record/associations/association.rb:204:in `raise_on_type_mismatch'
  activerecord (3.2.13) lib/active_record/associations/has_one_association.rb:8:in `replace'
  activerecord (3.2.13) lib/active_record/associations/singular_association.rb:17:in `writer'
  activerecord (3.2.13) lib/active_record/associations/builder/association.rb:51:in `block in define_writers'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
  activerecord (3.2.13) lib/active_record/persistence.rb:216:in `block in update_attributes'
  activerecord (3.2.13) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
  activerecord (3.2.13)    lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
  activerecord (3.2.13) lib/active_record/transactions.rb:208:in `transaction'
  activerecord (3.2.13) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
  activerecord (3.2.13) lib/active_record/persistence.rb:215:in `update_attributes'
  app/controllers/properties/build_controller.rb:23:in `update'

物业模型

class Property < ActiveRecord::Base
  attr_accessible  :name, :address_attributes, :tenants_attributes, :meter_attributes,  :council_history, :council_history_attributes, :property_id, :council_id, :status
  belongs_to :user 

  has_one :council_history
  accepts_nested_attributes_for :council_history

  has_one :council, through: :council_history
  accepts_nested_attributes_for :council

end

理事会模式

class Council < ActiveRecord::Base
  attr_accessible :CouncilEmail, :name, :CouncilTel

  has_many   :council_history

  has_many   :properties, :through => :council_history 
end

理事会历史模型

 class CouncilHistory < ActiveRecord::Base
  attr_accessible :council_id, :property_id, :vacant 

  belongs_to :council

  belongs_to :property

 end

选择理事会视图表格

<h1>Select Council</h1>



<%= simple_form_for @property, :url => url_for(:action => 'update', :controller =>   'properties/build'), :method => 'put' do |f| %>
  <%= f.simple_fields_for :council_history do |builder| %>
  <%= builder.input :id, :collection => Council.all %>
  <%= builder.submit %>
 <% end %>
<% end %>

构建属性控制器

class Properties::BuildController < ApplicationController
  include Wicked::Wizard

   steps :tenant, :meter, :council, :confirmed 

  def show
    @property = Property.find(params[:property_id])
    @tenants = @property.tenants.new(params[:tenant_id])
    @meter = @property.build_meter
    @council = @property.build_council_history
    render_wizard
  end

  def edit
    @property = Property.find(params[:property_id])
  end


  def update
    @property = Property.find(params[:property_id])
    params[:property][:status] = step.to_s
    params[:property][:status] = 'active' if step == steps.last
    @property.update_attributes(params[:property])
    render_wizard @property
  end
end
答案

在你的Property模型中你有attr_accessible :council_history,尝试删除它。

我没有测试这个,但我相信问题是因为你有has_one :council_historysimple_fields_for使用这个:council_history而不是生成council_history_attributes参数。

另一答案

在我的情况下,它与我如何命名我的强参数严格相关。

model_attributes,其中model是您的实际型号名称。以防这是我的代码:

父控制器强对称定义

def well_params params.require(:well).permit(:name, :well_number, :api_number, :company_id, :permit_date, :spud_date, :inclination, :formation, :lateral_length, :lateral_depth, :measured_depth, stages_attributes: [:id, :stage_number, :stage_bottom, :start_date, :duration, :fluid, :proppant, :project_id, :created_at, :updated_at, :stage_top,:well_id] ) end

父模型def:

class Well < ApplicationRecord has_many :stages accepts_nested_attributes_for :stages ...

儿童模特def:

class Stage < ApplicationRecord belongs_to :well ...

你可以看到我的修复只是简单地重命名现在有stages_attributes,在它只是stageswell_stages之前,你不能这样做而不是必须像它在snipet stages_attributes

希望这可以帮助!

PS:我在here找到了解决方案,它描述了如何编写复杂表单,如果你看一下表单动作的输出,基本上告诉你使用它,因为我在描述

{'person'=> {'name'=>'John Doe','addresses_attributes'=> {'0'=> {'kind'=>'Home','street'=>'221b Baker Street'}, '1'=> {'kind'=>'办公室','街道'=> '31 Spooner Street'}}}}

它会告诉你再次使用addresses_attributes ... xxx_attributes

以上是关于对象需要,得到ActiveSupport :: HashWithIndifferentAccess嵌套表单的主要内容,如果未能解决你的问题,请参考以下文章

如何在数据库中获取原始的“created_at”值(不是转换为 ActiveSupport::TimeWithZone 的对象)

Rails:我如何需要 ActiveSupport 的rescue_from 方法?

类方法上的 ActiveSupport::Callbacks

ActiveSupport::Memoizable 指的是哪种 Ruby memoize 模式?

尝试加载 gem 'devise. ActiveSupport:持续时间不能强制转换为整数

带 ActiveSupport 的默认时区(不带 Rails)