I am new to Ruby. I am writing a Restful API application using Rails 4. How can I return a 404 JSON not found string when the record is not found?
I found a number of posts but no luck, only for Rails 3.
In my controller I can caught the exception
def show
country = Country.find(params[:id])
render :json => country.to_record
rescue Exception
render :json => "404"
end
But I want a generic one to capture all the not found resources.
Use
rescue_from
. See http://guides.rubyonrails.org/v2.3.11/action_controller_overview.html#rescueIn this instance use something like:
Do: