Forum: Ruby on Rails NoMethodError in User_photos#show

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.
Ken L. (Guest)
on 2009-02-12 11:32
I'm following practical rails social networking sites book. While
creating a phoyo gallery, I've encountered this error:

NoMethodError in User_photos#show
Showing user_photos/show.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.user

Extracted source (around line #2):

1: <h3>
2:   <%= link_to "#{@photo.user.username}'s Photos",
3:                   user_new_photo_path(:user_id => @photo.user) %>
4: </h3>
5:


What should I do?
MaD (Guest)
on 2009-02-12 11:51
(Received via mailing list)
well, the error-message is pretty clear:
> The error occurred while evaluating nil.user
> 2:   <%= link_to "#{@photo.user.username}'s Photos",

@photo seems to be nil. the reason for it can be found in your
controller (where show is defined).
Ken L. (Guest)
on 2009-02-14 05:32
Attachment: user_photos_controller.rb (0 Bytes)
MaD wrote:
> well, the error-message is pretty clear:
>> The error occurred while evaluating nil.user
>> 2: � <%= link_to "#{@photo.user.username}'s Photos",
>
> @photo seems to be nil. the reason for it can be found in your
> controller (where show is defined).


I still have no clue at all as I follow the code from the book. The
application trace state that the "respond to do |format|" in the show
method has something wrong.

Below is controller where show is defined:

Class UserPhotosController < ApplicationController
  before_filter :login_required, :except => [:index, :show]

  def index
    @user = User.find(params[:user_id])
    @photo_pages = Paginator.new(self, @user.photos.count, 9,
params[:page])
    @photos = @user.photos.find(:all, :order => 'created_at DESC',
                                      :limit =>
@photo_pages.items_per_page,
                                      :offset =>
@photo_pages.current.offset)
    respond_to do |format|
      format.html # index.rhtml
      format.xml { render :xml => @photos.to_xml }
    end
  end

  def show
    @photo = Photo.find_by_user_id_and_id(params[:user_id],
                                          params[:id],
                                          :include => :user)

    respond_to do |format|
      format.html # show.rhtml
      format.xml { render :xml => @photo.to_xml }
    end
  end
  .
  .
  .
Frederick C. (Guest)
on 2009-02-14 13:08
(Received via mailing list)
On Feb 14, 3:32 am, Ken L. <removed_email_address@domain.invalid> wrote:
> method has something wrong.
>
> Below is controller where show is defined:
>
Not necessarily. For example if the link you generated was not quite
right so that params[:user_id] or params[:id] were not correct then
Photo.find_by_user_id_and_id might quite legitimately return nil.

Fred
MaD (Guest)
on 2009-02-14 13:53
(Received via mailing list)
two suggestions:
1. set a debugger at the beginning of you show-method. that way you
will find the reason for @photo being nil.
2. since Photo.id is your primary key (and therefore unique) you could
just do your find like this:
  @photo = Photo.find(params[:id])
instead of the much longer:
  @photo = Photo.find_by_user_id_and_id(params[:user_id], params
[:id], :include => :user)
as long as id distinctly identifies the record the user_id doesn't
matter.
Ken L. (Guest)
on 2009-02-16 08:52
MaD wrote:
> two suggestions:
> 1. set a debugger at the beginning of you show-method. that way you
> will find the reason for @photo being nil.
> 2. since Photo.id is your primary key (and therefore unique) you could
> just do your find like this:
>   @photo = Photo.find(params[:id])
> instead of the much longer:
>   @photo = Photo.find_by_user_id_and_id(params[:user_id], params
> [:id], :include => :user)
> as long as id distinctly identifies the record the user_id doesn't
> matter

The above method doesnt work. Any other solutions? Does it got to do
with my photo controller?

class PhotosController < ApplicationController
  def index
    photos_count = Photo.count(:conditions => 'thumbnail IS NULL')
    @photo_pages = Paginator.new(self, photos_count, 9, params[:page])
    @photos = Photo.find(:all,
                         :conditions => 'thumbnail IS NULL',
                         :order => 'created_at DESC',
                         :limit => @photo_pages.items_per_page,
                         :offset => @photo_pages.current.offset)
  end
end
MaD (Guest)
on 2009-02-16 13:21
(Received via mailing list)
it's always hard to tell what's wrong with other people's code, but
let's give it another try:
- you said a simple Photo.find(params[:id]) wouldn't work. well, that
could mean either params[:id] is not set or set to a value that
doesn't exist. did you set a debugger and look at your params? what
did it tell you?

- i gotta stress this point once more: make use of the (really great)
debugger! that way you can see what's going on inside your code. set a
debugger, point your browser to that location, launch irb and hack
along!
Ar C. (Guest)
on 2009-02-16 19:51
Ken L. wrote:
> The above method doesnt work. Any other solutions? Does it got to do
> with my photo controller?

Maybe yes, maybe no.

1. Does the index view work correctly (do you get the index page with 9
photos)?

2. When you view the source for your index page, what does the link_to
url look like for a given photo?

3. When selecting that link hits your show method, what is params[:id]?

4. Given you have a valid params[:id] in the show method, why not just a
simple
  @photo = Photo.find(:first, blah blah conditions blah blah)

5. You can also put error trapping code in your controller to catch your
possible ActiveRecord error

Step by step triage is a very general, very valuable, very necessary
skill...
Ken L. (Guest)
on 2009-02-17 09:19
MaD wrote:
> it's always hard to tell what's wrong with other people's code, but
> let's give it another try:
> - you said a simple Photo.find(params[:id]) wouldn't work. well, that
> could mean either params[:id] is not set or set to a value that
> doesn't exist. did you set a debugger and look at your params? what
> did it tell you?
>
> - i gotta stress this point once more: make use of the (really great)
> debugger! that way you can see what's going on inside your code. set a
> debugger, point your browser to that location, launch irb and hack
> along!

I manage to solve it, but a new problem arises. This time it happens
when I tried to upload a photo.

NameError in User photosController#index

uninitialized constant UserPhotosController::Paginator

RAILS_ROOT: C:/INSTAN~1/rails_apps/museum

Application Trace
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/dependencies.rb:478:in
`const_missing'

app/controllers/user_photos_controller.rb:6:in `index'

What should I do?
MaD (Guest)
on 2009-02-17 10:07
(Received via mailing list)
well, it seems Paginator is not defined. is pagination working for you
in other places?
Ken L. (Guest)
on 2009-02-17 10:48
MaD wrote:
> well, it seems Paginator is not defined. is pagination working for you
> in other places?

Im not sure whether pagination is working elsewhere. How to check that?
and im using rails version 2.02. Is the classic pagination method
depreciated? If so, what to change for my controller?

@photo_pages = Paginator.new(self, @user.photos_count, 9, params[:page])
This topic is locked and can not be replied to.