views:

95

answers:

2

I have read through the Rails docs for Routing, Restful Resources, and the UrlHelper, and still don't understand best practices for creating complex/nested routes. The example I'm working on now is for events, which has_many rsvps. So a user's looking through a list of events, and clicks register, and goes through a registration process, etc. I want the urls to look like this:

/events
/events/123 # possible without title, like SO
/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this
/events/123/my-event-title/registration/new
... and all the restful nested resouces.

Question is, how do I accomplish this with the minimal amount of code?

Here's what I currently have:

map.resources :events do |event|
  event.resources :rsvps, :as => "registration"
end

That gets me this:

/events/123/registration

What's the best way to accomplish the other 2 routes?

/events/123/my-event-title # canonical version
/events/my-category/123/my-event-title # also possible like this

Where my-category is just an array of 10 possible types the event can be.

I've modified Event#to_param to return "#{self.id.to_s}-#{self.title.parameterize}", but I'd prefer to have /id/title with the whole canonical-ness

A: 

Have you looked at the Rails routing guide? It's got lots of info to get you understanding the router and includes a section on nested resources.

bensie
+1  A: 

Because the SEO-friendly URLs you describe do not conform to the way Rails generates RESTful routes, you will need to use regular routes to set these up. For example:

map.show_event 'events/:id/:event_title', :controller => 'events', :action => 'show'

Don't feel that you have to use the automatically generated RESTful routes for absolutely everything in your application. It's not always a good fit for what you're trying to do. Even the Rails routing guide says so:

While RESTful routing has become the Rails standard, there are still plenty of places where the simpler regular routing works fine. You can even mix the two styles within a single application. In general, you should prefer RESTful routing when possible, because it will make parts of your application easier to write. But there’s no need to try to shoehorn every last piece of your application into a RESTful framework if that’s not a good fit.

Jimmy Cuadra
okay cool, that's basically all I was wondering, "is it okay to not use map.resources?". Thanks for confirming that. I'll do what you described :).
viatropos