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: ("")
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)
Try something like this in your controller method:
if flickr.photos.size < 1
flash[:notice] = “Golly, there’s nothing out there that matches” +
params[:tags] + “.”
else
render(:partial => “photo”, :collection => flickr.photos(:tags =>
params[:tags], :per_page => ‘28’))
etc. (if there’s more)
end
Some others may chime in with more Rubyish ways of composing the
message, but the main idea is intercept the empty set before you even
get to the render command. This particular solution keeps the same page
up but puts a flash message at the top.
flash[:notice] = @photos.nil? search for tags ‘platinotype’ which at
present returns 8 results and @photos.nil? equals ‘false’; meaning it
is NOT nil, which is what one would expect.
However…
change ‘platinotype’ to ‘platinotype1212’; which should return NIL
meaning that @photos.nil? equals ‘true’ and I’m right back where I
started…
“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”
I’d expected that the @photos.nil? would return ‘false’ if the
collection was populated and ‘true’ if empty, but this seems not to be
the case. How on earth is one supposed to handle conditional statement
based on a simple boolean if only half the data is there?
Or am I missing some simple and vital information on handling
collections in RoR?
Yep, I tried this. It looks like it should work and work well. But I
still get:
NoMethodError in FlickrController#search
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
If I search for ‘platinotype’ I get 8 results and it all works. If I
search for ‘platinotype123123’ (which I know won’t return results) I
get the above error and no matter how I try to get the length or
content of the array I can’t get past the nil.collect error as above.
I’m wondering if it has to do with the flickr.rb (gem) and it not
handling back nil (empty) arrays? (Does my being on windows affect this
at all? There seem to be more recent gems for non-windows platforms.)
Thanks for the help! I’ll keep reading in search of the answer.
I’m wondering if it has to do with the flickr.rb (gem) and it not
handling back nil (empty) arrays? (Does my being on windows affect this
at all? There seem to be more recent gems for non-windows platforms.)
Thanks for the help! I’ll keep reading in search of the answer.
b/
…three years later, I’m still running into this problem, and I have the
same suspicion: there’s an internal error in the flickr gem when the
search turns up nothing.
By now, you must have given up on the gem or found a better solution.
for flickr with RoR. I’ll continue searching for a solution to this
problem for now.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.