gpt4 book ai didi

ruby-on-rails - 未定义方法 `[]=' 对于 nil :NilClass

转载 作者:行者123 更新时间:2023-12-02 05:40:58 33 4
gpt4 key购买 nike

当我尝试提交带有嵌套属性的表单时,我不断收到此未定义的错误,不确定它来自哪里,现在已经与它搏斗了很长一段时间,我试图让用户在理事会模型中选择一个选项提交他们的选择,我不确定我的关联是否正确连接,或者错误是否来自表单。我是 Rails 新手。提前致谢。

错误 已更新

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:

理事会 View

<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 %>

Controller

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

理事会.rb

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

belongs_to :property
end

已更新 Propery.rb

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

最佳答案

我认为是这样

params[:property]

nil。所以Ruby在做的时候会提示

params[:property][:status] = 'foo'

你可能想做这样的事情:

if params[:property]
params[:property][:status] = 'foo'
end

但是,在您的情况下,问题是因为您使用的是 form_tag 而不是 form_for,因此 params[:property] 不是已定义。

关于ruby-on-rails - 未定义方法 `[]=' 对于 nil :NilClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18296970/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com