Nil objects that are not nil elsewhere in same scope


#1

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


#2

is @activity nil for example ?


#3

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

I am trying to get something like

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


#4

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


#5

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

I am trying to get something like

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