Announcement (2017-05-07): www.ruby-forum.com is now read-only since I
unfortunately do not have the time to support and maintain the forum any
more. Please see rubyonrails.org/community and ruby-lang.org/en/community
for other Rails- und Ruby-related community platforms.

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: ("<img
class="photo" src="<%= photo.sizes[0]['source'] %>">")
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.
B/

Hi Bjorn,
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.
Shauna

Well, I tried this, it seems like a sensible strategy and all but it
doesn't allow me to intercept NIL results in the collection.
class FlickrController < ApplicationController
def search
flickr = Flickr.new 'fcaadf470f78d2f8bd8098a1411fb687'
@photos = flickr.photos(:tags => params[:tags],:per_page => '28')
if flickr.photos.size < 1
flash[:notice] = "Golly, nothing that matched" + params[:tags] +
"."
else
render(:partial => "photo",:collection => @photos)
end
end
end
searching for "peach2323" (tags) results in:
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
When what one might expect a nice polite message flashed to screen as
per the controller.
I'll keep trying...the answer is out there somewhere. It seems somehow
to hinge on 'nil.collect'.
B/

Interesting but confounding behavior here...
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?
B/

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.
b/

bjorn wrote:
> 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.

Actually, I was able to fix it by modifying the flickr.rb file from the
gem at line 109. replaced it with the following:
if collection
collection.collect { |photo| Photo.new(photo['id'], @api_key) }
else
return nil
end