Forum: Ruby on Rails (Newbie) routing question

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.
Vahagn H. (Guest)
on 2008-10-28 12:48
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: <li>
2:    <%= link_to image_tag(photo.public_filename('thumb')),
user_photo_path(:user_id => photo.user, :id => photo) %>
3: </li>

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
Bobnation (Guest)
on 2008-10-28 15:14
(Received via mailing list)
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-
Vahagn H. (Guest)
on 2008-10-29 16:11
@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-
This topic is locked and can not be replied to.