【问题标题】:Uknown attributes error nested attributes未知属性错误嵌套属性
【发布时间】:2018-03-30 23:14:48
【问题描述】:

我不断收到未知属性错误,尽管我在这里尝试了一些与此问题类似的答案。我正在尝试使用嵌套表单将理事会和属性属性更新到连接表理事会历史记录中,当我尝试加载视图表单时出现此错误,请告知您认为我可能做错了什么,这是对 Rails 和编程的新手。

错误

Started PUT "/properties/6/build/council" for 127.0.0.1 at 2013-08-19 17:35:45 +0100
Processing by Properties::BuildController#update as HTML
  Parameters: {"utf8"=>"✓",     "authenticity_token"=>"wBWQaxtBioqzGLkhUrstqS+cFD/xvEutXnJ0jWNtSa0=", "property"=>   {"council_history_attributes"=>{"council_id"=>""}}, "commit"=>"Create 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 6ms

ActiveRecord::UnknownAttributeError - unknown attribute: council_history_attributes:
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:88:in `block in  assign_attributes'
  activerecord (3.2.13) lib/active_record/attribute_assignment.rb:78: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:24: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

  has_one :council, through: :council_history, :foreign_key => :council_id
  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, :foreign_key => :property_id
end

议会历史模型 -- 加入表

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

  belongs_to :council

  belongs_to :property

end

*查看 *

<%= simple_form_for @property, :url => url_for(:action => 'update', :controller =>    'properties/build'), :method => 'put' do |f| %>
  <%= f.simple_fields_for :council do |builder| %>
      <%= builder.input :council_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
    @council_history = @property.council_history.build
    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

【问题讨论】:

  • accepts_nested_attributes_for :council_history 放入您的Council 模型中。
  • 这不起作用,仍然出现同样的错误。
  • 抱歉,请尝试将其放入您的 Property 模型中,因为这是导致错误的控制器。
  • 感谢 @mbratch 成功了,但现在我收到了针对议会的大规模分配错误,即使我将它放在了可访问的 attr 中。

标签: ruby-on-rails ruby ruby-on-rails-3


【解决方案1】:

在property.rb中

添加accepts_nested_attributes_for :council_history

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

  has_one :council, through: :council_history, :foreign_key => :council_id
  accepts_nested_attributes_for :council
  
  accepts_nested_attributes_for :council_history #add this line

 end

【讨论】:

    【解决方案2】:

    感谢Muntasim的回答。 我无法直接评论已接受的答案,但想更新如何做到这一点,因为在 Rails 中引入了 strong parameters,现在 attr_accessible 不再在 Rails(最新版本 v2.3.8)中4+。

    控制器中的强参数,

    def person_params
      params.permit(:name, {:emails => []}, :friends => [ :name, { :family => [ :name ], :hobbies => [] }])
    end
    

    可以使用强参数outside of controllers(这是我遇到的问题导致我来到这里),

    raw_parameters = { :email => "john@example.com", :name => "John", :admin => true }
    parameters = ActionController::Parameters.new(raw_parameters)
    user = User.create(parameters.permit(:name, :email))
    

    或者,如果您想使用 attr_accessible,您仍然可以,请参阅answer

    如果要使用attr_accessible,需要添加 protected_attributes 到您的 Gemfile。否则,你将面临 带有 RuntimeError。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      相关资源
      最近更新 更多