2013-08-18 196 views
2

當我試圖提交這個嵌套屬性的表單時,我不斷收到這個未定義的錯誤,不知道它從哪裏來的摔跤了很長一段時間,我試圖讓用戶在理事會模型中選擇一個選項提交他們的選擇,我不確定是否我的關聯正確連接或錯誤來自表單。我不喜歡鐵軌。提前致謝。未定義的方法`[] ='爲零:NilClass

錯誤更新

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: 

會查看

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

控制器

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

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 
+1

錯誤回溯會有幫助。 –

回答

5

我覺得這

params[:property] 

nil。因此,紅寶石抱怨做

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

時,您可能想要做這樣的事情:

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

不過你的情況的問題是因爲您使用的是form_tag代替form_for,爲此params[:property]不定義。

+0

這是正確的pierre-louis,試圖做出這樣的改變,出現了同樣的錯誤,你認爲它的必要性我必須在表單中包含狀態,我正在使用狀態對本教程後的多步表單進行部分驗證[鏈接](http://goo.gl/mKDxey) – cyclopse87

+0

在你的教程中,'status'變量來自模型。在你的情況下,你必須將它包含在表單或模型中,如教程所示;) –

+0

已經在模型中包含變量狀態,就像在教程中一樣,我仍然得到相同的錯誤。在我的問題中更新了屬性模型。 – cyclopse87

0

一種更好的方法來檢查紅寶石哈希嵌套屬性現在是使用

例子:

params.dig(:property, :status) 

如果該鍵沒有定義爲零返回。

相關問題