Hi, I’m just getting going with RoR and thus far am enjoying it very
much. There’s just something oddly satisfying about it. That being said
I do find myself tripping over the little things…like NIL.
I’ve been doing a tutorial on building a flickr tag search and noticed
that it doesn’t handle NIL results well; actually, it doesn’t handle
errors at all.
My controller contains this: (it renders a partial with a collection of
images from flickr) The partial is just an image tag: ("")
render(:partial => “photo”, :collection => flickr.photos(:tags =>
params[:tags], :per_page => ‘28’)) unless :collection.nil?
Which I’m assuming is completely wrong since I still get this:
You have a nil object when you didn’t expect it!
You might have expected an instance of Array.
The error occured while evaluating nil.collect
When entering a tag like “peachqwwerwer” as a search term.
My question is this. If you know that there’s a good likelihood that
you’re going to get back NIL records then what and where is the best
(most effective/flexible) place to handle it and what is the general
best practice for error handling like this?
I’d like to show the user a nice looking message “Golly, there’s
nothing out there that matches “peachwwerwer”.” (etc)
Many thanks in advance.