NoMethodError in User_photos#show

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).

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

Attachments: http://www.ruby-forum.com/attachment/3296/user_photos_controller.rb

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:

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

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.

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

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!

Ken Lim 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...

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?

well, it seems Paginator is not defined. is pagination working for you in other places?

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])