# (Newbie) routing question

**URL:** <https://www.ruby-forum.com/t/newbie-routing-question/151956>\
**Category:** Rails\
**Created:** [October 28, 2008, 11:48am UTC](https://www.ruby-forum.com/t/newbie-routing-question/151956 "2008-10-28T11:48:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 28, 2008, 11:48am UTC](https://www.ruby-forum.com/t/newbie-routing-question/151956/1 "2008-10-28T11:48:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 28, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/newbie-routing-question/151956/2 "2008-10-28T14:14:16Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![vahagn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vahagn](https://www.ruby-forum.com/u/vahagn)\
**Post date:** [October 29, 2008, 3:11pm UTC](https://www.ruby-forum.com/t/newbie-routing-question/151956/3 "2008-10-29T15:11:44Z")

</div>

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