# Nil objects that are not nil elsewhere in same scope

**URL:** <https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291>\
**Category:** Rails\
**Created:** [May 28, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291 "2007-05-28T14:20:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 28, 2007, 2:20pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291/1 "2007-05-28T14:20:13Z")

</div>

hi there

how can an object be nil in line 37 but not nil in lines 34 & 40???

Parameters: {“activity\_id”=\>“7”, “action”=\>“show”, “id”=\>“15”,  
“controller”=\>“photos”}  
User Columns (0.016000) SHOW FIELDS FROM users  
User Load (0.000000) SELECT \* FROM users WHERE (users.`id` = 1)  
LIMIT 1  
Activity Columns (0.000000) SHOW FIELDS FROM activities  
Activity Load (0.016000) SELECT \* FROM activities WHERE  
(activities.`id`  
= 7)  
Photo Columns (0.016000) SHOW FIELDS FROM photos  
Photo Load (0.000000) SELECT \* FROM photos WHERE (photos.`id` = 15  
AND (  
photos.activity\_id = 7))  
Rendering template within layouts/application  
Rendering photos/show

ActionView::TemplateError (You have a nil object when you didn’t expect  
it!  
The error occurred while evaluating nil.to\_sym) on line #37 of  
app/views/photos/show.html.erb:  
34: \<%= link\_to image\_tag(@photo.public\_filename(:thumb)) %\>  
35:  
36:   
  
37: \<%= link\_to “Download #{@photo.width} X #{@photo.height}”,  
download\_photos\_url(@photo, @activity) %\>  
38:  
39:   
  
40: \<%= link\_to ‘Edit’, activity\_edit\_photo\_url(@activity, @photo) %\> |

* * *

This is photos\_controller =\> show

def show  
#@photo = Photo.find(params[:id])  
@photo = @activity.photos.find(params[:id])

```
respond_to do |format|
  format.html # show.html.erb
  format.xml { render :xml => @photo }
end

```

end

* * *

cheers

dion

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 28, 2007, 2:23pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291/2 "2007-05-28T14:23:50Z")

</div>

is @activity nil for example ?

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 28, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291/3 "2007-05-28T14:42:17Z")

</div>

no,

I can do

\<%= activity\_photo\_url(@activity, @photo)%\>

in the line above the one that breaks and it works fine

I think my named route (download\_photos\_url) does not like taking  
parameters, but am not sure why?!??!!

\<%= link\_to “Download”, download\_photos\_url(@activity, @photo) %\>

[http://localhost:2000/activities/7/photos/15](http://localhost:2000/activities/7/photos/15)

I am trying to get something like

[http://localhost:2000/activities/7/photos/download/15](http://localhost:2000/activities/7/photos/download/15)

this is my routes.rb

map.resources :activities do |activity|  
activity.resources :photos, :collection =\> {:download =\> :post }  
end

map.resources :photos, :collection =\> {:download =\> :post }

cheers

dion

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 28, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291/4 "2007-05-28T16:12:29Z")

</div>

jean,

thanks big time

works great

VIEW  
\<%= link\_to “Download”, activity\_download\_photo\_url(@activity) %\>

ROUTES.RB  
map.resources :activities do |activity|  
activity.resources :photos, :member =\> {:download =\> :get }  
end

map.resources :photos, :member =\> {:download =\> :get }

after looking at the server log, GET is obvious, but the strange thing  
is  
that I have had this working without nested resources using POST…i’ll  
have  
to check that app again…

also member vs collection was the trick…which makes total  
sense…sometimes you just can’t see the forest for the trees

once again, cheers heaps

you saved me a lot of troubles

dion

---

<div class="post-metadata">

**Author:** ![Dion\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/839c29/32.png) [@Dion\_H](https://www.ruby-forum.com/u/Dion_H)\
**Post date:** [May 28, 2007, 3:11pm UTC](https://www.ruby-forum.com/t/nil-objects-that-are-not-nil-elsewhere-in-same-scope/105291/5 "2007-05-28T15:11:47Z")

</div>

So, the problem is the following:

map.resources :photos, :collection =\> {:download =\> :post }

should be:

map.resources :photos, :member =\> {:download =\> :post }

since you pass the id of the photo (/activity/1/photos;download would be  
the URI for the collection)

Why are you using a POST instead of a GET ?

Dion H. wrote:

> no,
> 
> I can do
> 
> \<%= activity\_photo\_url(@activity, @photo)%\>
> 
> in the line above the one that breaks and it works fine
> 
> I think my named route (download\_photos\_url) does not like taking  
> parameters, but am not sure why?!??!!
> 
> \<%= link\_to “Download”, download\_photos\_url(@activity, @photo) %\>
> 
> [http://localhost:2000/activities/7/photos/15](http://localhost:2000/activities/7/photos/15)
> 
> I am trying to get something like
> 
> [http://localhost:2000/activities/7/photos/download/15](http://localhost:2000/activities/7/photos/download/15)
> 
> this is my routes.rb
> 
> map.resources :activities do |activity|  
> activity.resources :photos, :collection =\> {:download =\> :post }  
> end
> 
> map.resources :photos, :collection =\> {:download =\> :post }
> 
> cheers
> 
> dion
