(Newbie) routing question


#1

Hi, I have a question about routing and nested resources. I have an app
with Photos that belong to Users via a many2one relationship. The URL:

http://localhost:3000/photos

lists all photos (from all users). Clicking on a random photo from this
listing should send one to f ex:

http://localhost:3000/users/2/photos/10

Because I have this in my routes.rb:

map.resources :users, :member => { :enable => :put } do |users|
users.resources :photos, :name_prefix => ‘user_’,
:controller => ‘user_photos’,
:member => { :add_tag => :put, :remove_tag
=> :delete }
end

However when I load http://localhost:3000/photos I get this error
message:

user_photo_url failed to generate from {:controller=>“user_photos”,
:action=>“show”, :user_id=>nil, :id=>#<Photo id: 2, user_id: nil, title:
nil, body: nil, created_at: “2008-10-27 14:26:32”, etc

Extracted source (around line #2):

1:


  • 2: <%= link_to image_tag(photo.public_filename(‘thumb’)),
    user_photo_path(:user_id => photo.user, :id => photo) %>
    3:
  • The above partial, _photo.rhtml, is what causes the error.

    This code has worked fine up until I refactored it to function with
    will_paginate instead of classic_pagination. I haven’t touched the
    routing declarations or the _photo.rhtml partial.

    Any help will be appreciated!

    / Vahagn


    #2

    Looking at the error message, it seems to be saying that user_id is
    nil … which tells me that you don’t have a user set for that photo
    and it can’t generate the URL without knowing the id of the user.

    On Oct 28, 5:48 am, Vahagn H. <rails-mailing-l…@andreas-


    #3

    @Bobnation:

    Thanks - this is also what I take home from the error message - I just
    don’t understand where the user for a photo should be instantiated. In
    my photos_controller, I have

    @photos = Photo.paginate_by_sql [‘select * from photos’],
    :page => params[:page],
    :per_page => 10

    this uses will_paginate and it fails like I described in the first post.
    However if I switch to this:

    #old controller, uses classic_pagination
    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
    )

    Then the photos and the generated urls work fine, but the pagination
    links don’t.

    / Vahagn

    Bobnation wrote:

    Looking at the error message, it seems to be saying that user_id is
    nil … which tells me that you don’t have a user set for that photo
    and it can’t generate the URL without knowing the id of the user.

    On Oct 28, 5:48�am, Vahagn H. <rails-mailing-l…@andreas-