2014-09-20 29 views
0

下面的代碼在我嘗試加載時會一直拋出一個NoMethodError。未定義的方法`foo'爲零:NilClass - 引發錯誤,但在rails控制檯中工作

<% pstate= Place.where(statename: @agency.state) %> 
<% fips_place = pstate.where(placeshort: @agency.city) %> 
<%= place = 'PLACE:' + fips_place.first.placefp %> 

但它在控制檯當我運行正常工作:

> pstate = Place.where(statename: "CA") 
> fips_place = pstate.where(placeshort: "Los Angeles") 
> place = "PLACE: " + fips_place.first.placefp 
=> "PLACE: 85292" 

控制器/ agencies_controller.rb:直到重置我的數據庫。按照前面的工作

def set_agency 
    @agency = Agency.find(params[:id]) 
end 

型號/ agency.rb:

class Agency < ActiveRecord::Base 
    has_one :place 
    ... 
end 

型號/ place.rb:

class Place < ActiveRecord::Base 
    belongs_to :agency 
end 
+0

你的問題是什麼? – sawa 2014-09-20 18:09:08

+0

我收到錯誤!我不知道爲什麼。 – 2014-09-20 18:09:51

+0

代碼中\ @ agency.state和\ @ agency.city的值是什麼? – Lexsys 2014-09-20 18:10:10

回答

1

我要出去肢體和猜測,有在地方臺,其statenameplaceshort匹配@agency.state@agency.city沒有行。

那麼fips_place.first返回零,你試圖撥打placefp會導致錯誤。

它在控制檯中工作,因爲您對statenameplaceshort使用了不同的值。

+0

Aha!答對了。這不起作用,因爲我試圖在瀏覽器(本例中爲AK)的'@ agency'調用'placeshort'的值是'Anchorage Municipality'而不是'Anchorage'。 (在我重置我的數據庫之前它正在工作,因爲我之前抓住它並修復它。)謝謝! – 2014-09-20 18:20:14

相關問題