CountriesController #home中的ActiveRecord :: RecordNotFound

时间:2014-05-08 17:24:03

标签: ruby-on-rails activerecord

我可以传入一个身份证号码但是当我有下面的代码时,我会收到错误:

  

在CountriesController #home

中的ActiveRecord :: RecordNotFound
 def home
   @pin = Country.last
   @countries = Country.all
   #<the issue is on this line below>
   @country = Country.find(params[:country_id])
   gon.lat = @pin.latitude
   gon.lon = @pin.longitude
   gon
   gon.countries = @countries
 end

我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果

find在数据库中找不到具有该ID的ActiveRecord::RecordNotFound,则会引发Country错误。

你应该检查几件事:

  • params[:country_id]是否为nil?
  • 在终端中,运行rake routes,找到您的路线并检查其中是否确实存在:country_id参数。另请参阅routing documentation
  • 如果params[:country_id]不是nil,则数据库中可能没有这样的国家/地区。

有关详细信息,请参阅Rails documentation