下面的代码在我尝试加载时会一直抛出一个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
你的问题是什么? – sawa 2014-09-20 18:09:08
我收到错误!我不知道为什么。 – 2014-09-20 18:09:51
代码中\ @ agency.state和\ @ agency.city的值是什么? – Lexsys 2014-09-20 18:10:10