views:

248

answers:

2

I've got the following setup:

 map.resources :articles
 map.resources :users, :has_many => [:articles]

Which is fine as I can do /users/2/articles to get a list of articles by a given user.

I also have a route setup to do:

map.connect 'articles/month/:month/:year', :controller => 'articles', :action => 'index'

Which works for finding all articles for a given month and a given year.

What I want to do now is show articles by a given user for a given month and year.

Perhaps a url like: /users/2/articles/month/4/2009

Is there a neat way of doing this? Thanks

+1  A: 

I don't tend to use resource based routing, so in my mind the following should work

map.connect 'users/articles/:user_id/:month/:year', :controller => 'users', :action => 'articles', :user_id => 0, :month => Time.now.strftime("%m"), :year => Time.now.strftime("%Y")

this should allow a url of /users/articles/2/11/2009 and provides the current month and year as default values in case you want to use /users/articles/2 as a basic url and provide a month or year as extras when you want to be more specific.

It would route to the 'users' controller and 'articles' action with the other values as parameters in the params[] hash

Also, you should be able to change the order of the connect to:

map.connect 'users/:user_id/articles/:month/:year'

keeping the rest the same to get the url to be: /users/2/articles/11/2009

gablaxian
I've gone with map.connect 'users/:user_id/articles/:month/:year' which works fine. Thanks
sebastyuiop
+1  A: 
map.resources :user do |user|
  user.connect 'articles/:month/:year', :controller => 'articles'
end
Val
That's certianly better looking, thanks
sebastyuiop