Question

I can pass in an ID number but when I have the code below i get the error:

ActiveRecord::RecordNotFound in CountriesController#home

 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

What am I doing wrong?

Was it helpful?

Solution

find throws an ActiveRecord::RecordNotFound error if it could not find a Country with that ID in the database.

You should check a few things:

  • Is params[:country_id] nil?
  • In a terminal, run rake routes, find your route and check if there's indeed a :country_id parameter in it. Also see the routing documentation.
  • If params[:country_id] is not nil, there might not be such a country in your database.

See the Rails documentation for more information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top