views:

22

answers:

1

Hi

I'm working on a rails application which is built around a tree data structure. As such, the index of the controller displays the root node of said structure. Demonstration is probably easier to explain what I want:

/place/1 == place

can I restfully define such that

/place/1/photos == place/photos

and

/place/1/photos/1 == place/photos/1

etc?

Ideally what I'd like is that the articles url and its nested resource urls work by default such that I don't need to change a whole bunch of stuff and conditionally generate paths all over the place.

Thanks in advance for any help. :)

+1  A: 

You could have the usual nested restful routes (it's good to keep them as the baseline, and to use them for the non-sexy urls like update, create, etc) and then add some custom routes for your 'pretty' urls:

map.nested_photo "/place/photos/:id", :controller => "photos", :action => "show"
map.edit_nested_photo "/place/photos/:id/edit", :controller => "photos", :action => "edit"

you could then add a bit of logic into the photos controller to make sure you have the place even if you didn't get params[:place_id], eg

  if params[:place_id]
    @place = Place.find(params[:place_id])
  else 
    @place = @photo.place
  end

The main thing to watch out for in custom urls is that you don't create any conflict with a) the regular restful urls or b) your other custom urls.

Max Williams
Hi Max. That worked out great, thanks. :)
mark