I’ve got a blogs_controller with a Blog resource, so I’ve got your typical routes right now as follows:
/blogs/new
/blogs/1
/blogs/1/edit #etc
But here’s what I want:
/blogs/new
/blogs/2010/01/08/1-to_param-or-something
/blogs/2010/01/08/1-to_param-or-something/edit #etc
...
/blogs/2010/01 # all posts for January 2010, but how to specify custom action?
I know that I can do this with a combination of map.resources and map.connect, but I’ve got a lot of views that link to other pages via “new_blog_path” etc and I don’t want to have to go and edit those. Is this possible with map.resources alone? It might not be easy, but I’m not against being clever. I was thinking of something like:
map.resources :blogs, :path_prefix => ':year/:month/:day', :requirements => {:year => /\d{4}/, :month => /\d{1,2}/, :day => /\d{1,2}/}
But I’m not sure how that works with actions like ‘new’ or ‘create’, and it also gives me a route like /2010/01/08/blogs/1-to_param-etc with blogs in the middle of the URL.
So, is there a clever solution that I’m missing, or do I need to go the map.connect route?
I ran into the same issue recently, and, while this may not be what you’re looking for, this is what I’ve done to take care of it:
config/routes.rb:
blog_entries_controller.rb:
blog_entries_helper.rb:
_entry.html.erb:
and for the sake of completeness:
blog_entry.rb:
The #to_url method comes from rsl’s Stringex.
I’m still new to Rails (and programming) myself, but this is probably the simplest way to go about it. This isn’t a RESTful way of going about things so you don’t gain the benefit of map.resources, unfortunately.
I’m not sure (because I haven’t tried it), but you might be able to create the appropriate helpers in
application_helper.rbto override the default route helpers for blog_path, et al. If that works, then you won’t have to change any of your view code.If you’re feeling adventurous, you might check out Routing Filter. I considered using it, but it seems like overkill for this task.
Also, if you’re not aware, two things you can do to test your routes/paths from within script/console:
and
Good luck!