# Getting going

**URL:** <https://www.ruby-forum.com/t/getting-going/80653>\
**Category:** Rails\
**Created:** [December 29, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/getting-going/80653 "2006-12-29T17:24:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 29, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/getting-going/80653/1 "2006-12-29T17:24:42Z")

</div>

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.

B/

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 1:04am UTC](https://www.ruby-forum.com/t/getting-going/80653/2 "2006-12-30T01:04:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 5:24pm UTC](https://www.ruby-forum.com/t/getting-going/80653/3 "2006-12-30T17:24:13Z")

</div>

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/

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/getting-going/80653/4 "2006-12-30T21:00:58Z")

</div>

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/

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 5:29pm UTC](https://www.ruby-forum.com/t/getting-going/80653/5 "2006-12-30T17:29:49Z")

</div>

if @photos.size \< 1  
…

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 10:17pm UTC](https://www.ruby-forum.com/t/getting-going/80653/6 "2006-12-30T22:17:14Z")

</div>

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/

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [December 30, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/getting-going/80653/7 "2006-12-30T22:02:18Z")

</div>

Try:

class FlickrController \< ApplicationController  
def search  
flickr = Flickr.new ‘fcaadf470f78d2f8bd8098a1411fb687’  
@photos = flickr.photos(:tags =\> params[:tags],:per\_page =\> ‘28’)  
if @photos.size \< 1  
flash[:notice] = “Golly, nothing that matched” + params[:tags] +  
“.”  
else  
render(:partial =\> “photo”,:collection =\> @photos)  
end  
end  
end

– shauna

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [February 8, 2010, 7:16am UTC](https://www.ruby-forum.com/t/getting-going/80653/8 "2010-02-08T07:16:17Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![bjorn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/74df32/32.png) [@bjorn](https://www.ruby-forum.com/u/bjorn)\
**Post date:** [February 8, 2010, 7:10am UTC](https://www.ruby-forum.com/t/getting-going/80653/9 "2010-02-08T07:10:04Z")

</div>

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.
