首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >NilClass的未定义方法‘[]=’‘

NilClass的未定义方法‘[]=’‘
EN

Stack Overflow用户
提问于 2013-08-18 00:01:07
回答 3查看 9.9K关注 0票数 2

当我试图提交这个带有嵌套属性的表单时,我一直收到这个未定义的错误,我不知道它是从哪里来的--很长一段时间以来,我一直在与它搏斗,我试图让用户在理事会模型中选择一个选项--提交他们的选择,我不确定我是否正确地连接了我的关联,或者错误来自表单。我是铁轨的菜鸟。提前谢谢。

Error 更新

代码语言:javascript
运行
AI代码解释
复制
Properties::BuildController#update
app/controllers/properties/build_controller.rb, line 21

Started PUT "/properties/5/build/council" for 127.0.0.1 at 2013-08-18 08:52:07 +0100
Processing by Properties::BuildController#update as HTML
  Parameters: {"utf8"=>"✓","authenticity_token"=>"wBWQaxtBioqzGLkhUrstqS+cFD/xvEutXnJ0jWNtSa0=",  "council_id"=>"1", "commit"=>"Save changes", "property_id"=>"5", "id"=>"council"}
  Property Load (0.2ms)  SELECT "properties".* FROM "properties" WHERE "properties"."id" =  ? LIMIT 1  [["id", "5"]]
Completed 500 Internal Server Error in 35ms

NoMethodError - undefined method `[]=' for nil:NilClass:

理事会视图

代码语言:javascript
运行
AI代码解释
复制
<h1>Select Council</h1>


<%= form_tag  url_for(:action => 'update', :controller =>   'properties/build'), :method  => 'put' do %>

<%= select_tag :council_id, options_from_collection_for_select(Council.all, :id, :name) %>

    <%= submit_tag  %>
<% end %>

控制器

代码语言:javascript
运行
AI代码解释
复制
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
    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

Council.rb

代码语言:javascript
运行
AI代码解释
复制
class Council < ActiveRecord::Base
  attr_accessible :CouncilEmail, :name, :CouncilTel

  belongs_to :property
end

更新 Propery.rb

代码语言:javascript
运行
AI代码解释
复制
class Property < ActiveRecord::Base
  attr_accessible  :name, :address_attributes, :tenants_attributes, :meter_attributes, :council_attributes, :property_id, :status
  belongs_to :user 


  has_one :address, :as => :addressable
  accepts_nested_attributes_for :address, :allow_destroy => true


  has_one :council
  accepts_nested_attributes_for :council, :allow_destroy => true

  has_many :tenants, :inverse_of => :property
  accepts_nested_attributes_for :tenants, :allow_destroy => true, :reject_if     => :all_blank

  has_one :meter
  accepts_nested_attributes_for :meter, :allow_destroy => true


  validates :name,      :presence => :true
  validates :address,   :presence => :true
  validates :tenants,   :presence => true, :if => :active_or_tenants?
  validates :council,   :presence => true, :if => :active_or_council?


  def active?
    status == 'active'
  end

  def active_or_tenants?
    (status || '').include?('tenants') || active?
  end

  def active_or_council?
    (status || '').include?('council') || active?
  end
end
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-08-18 00:44:00

我想这是

代码语言:javascript
运行
AI代码解释
复制
params[:property]

nil。所以Ruby在做时会抱怨

代码语言:javascript
运行
AI代码解释
复制
params[:property][:status] = 'foo'

你可能想做这样的事情:

代码语言:javascript
运行
AI代码解释
复制
if params[:property]
  params[:property][:status] = 'foo'
end

但是,在您的示例中,问题在于您使用的是form_tag而不是form_for,因此没有为params[:property]定义。

票数 5
EN

Stack Overflow用户

发布于 2017-04-04 05:13:14

现在在ruby散列中检查嵌套属性的更好方法是使用dig

示例:

代码语言:javascript
运行
AI代码解释
复制
params.dig(:property, :status)

如果未定义密钥,则返回零。

票数 4
EN

Stack Overflow用户

发布于 2021-05-15 13:55:21

如果使用数据文件,请确保data.yml中没有偏离的data.yml请参阅此GitHub注释

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18296970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文