How can I prevent caching on exception? I have this action:
caches_page :index
...
def index
if params[:city]
city = City.find(params[:city])
@shows = city.shows
else
@shows = Show.all
end
...
If find crashed with ActionRecord::RecordNotFound nothing cached - it's ok. But I don't want this exception in my log file too. But if I:
begin
city = City.find(params[:city])
rescue ActiveRecord::RecordNotFound
render :nothing => true
return
end
Empty page cached!
What I suppose to do in this situation?
Try :
caches_page :index, :if => :city_exists?
private
def city_exists
city = City.find(params[:city])
!city.nil? ? true : false
end
And I think you can use action caching or fragment caching .
Take a look into details for Extending Caching.