views:

36

answers:

1

Is it possible to override a RESTful path?

For instance, I have photo_path(photo) which would generate /photos/12345

But I'd like for all uses of photo_path to actually generate URL's like /photos/joeschmoe/12345 (which is the photo's user and the photo id).

Obviously I could just create a new route, but wanted to make sure there wasn't a more RESTful way of doing it.

+1  A: 

You could make photos a sub-resource to user's, so you'd have users/joeschmoe/photos/12345 (of course here, your users controller would require the ability to accept a username instead of an id, which is another routing problem to solve but not difficult)

resources :users do
  resources :photos
end

Then your controller could maybe call

@photos = Photo.find_by_username(params[:id])

Although I think there are less hacky ways of doing that.

You could also add joeschmoe as a query string parameter.

Or you could make username an optional parameter in the route, so it would be something like

match "/photos(/:username)/:id" => "photos#show"

Or if you want a new named route:

match "/photos/:username/:id" => "photos#show_by_user", :as => :user_photo
Samo