How to customize RESTful Routes in Rails (basics)

Posted by viatropos on Stack Overflow See other posts from Stack Overflow or by viatropos
Published on 2010-03-18T22:49:54Z Indexed on 2010/03/19 0:01 UTC
Read the original article Hit count: 350

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

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about routes