Forum: Ruby on Rails nil objects that are not nil elsewhere in same scope

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.
Adebae130af75724f0296ff49958d6fb?d=identicon&s=25 Dion Hewson (Guest)
on 2007-05-28 14:20
(Received via mailing list)
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: <br>
37: <%= link_to "Download #{@photo.width} X #{@photo.height}",
download_photos_url(@photo, @activity) %>
38:
39: <br>
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
Eb778b8bb642aa8a82c2b8b63705b6af?d=identicon&s=25 Jean-Etienne Durand (jetienne)
on 2007-05-28 14:23
is @activity nil  for example ?
Adebae130af75724f0296ff49958d6fb?d=identicon&s=25 Dion Hewson (Guest)
on 2007-05-28 14:42
(Received via mailing list)
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
Eb778b8bb642aa8a82c2b8b63705b6af?d=identicon&s=25 Jean-Etienne Durand (jetienne)
on 2007-05-28 15:11
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 Hewson 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
Adebae130af75724f0296ff49958d6fb?d=identicon&s=25 Dion Hewson (Guest)
on 2007-05-28 16:12
(Received via mailing list)
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
This topic is locked and can not be replied to.