Topic: restful route for blog

i'm trying to stay restful, and so far it's been ok.

however, i want to achive the following and i'm not sure what the restful way is.

i have some blogs, for which there is a blog controller.

the index action retrieves an array of blogs and displays the summaries with links to the specific entries. clicking on a specific entry calls the show method against the blog id.


/blogs => index action showing summary
/blogs/1 => show action showing specific blog entry

now, i want to add archives which categorise by date


/blogs/archives/2007/03 => shows summary of blog posts for march 2007

what's the restful and resources way of doing this?

Re: restful route for blog

You would create an "archives" action in the blogs controller and then have the URL route to it appropriately:

# in routes.rb
map.archives "/blogs/archives/:year/:month", :controller => 'blogs', :action => 'archives'

# in controller
def archives
  @blogs = Blog.find(:all, :conditions => ['YEAR(created_at) = ? AND MONTH(created_at) = ?', params[:year], params[:month])
  render :action => 'index'

If you need the view to be significantly different from the index view, you should create a new view for it.

When linking to it you can use the named route:

archives_path(2007, 1)
# or
archives_path(:year => 2007, :month => 1)

Railscasts - Free Ruby on Rails Screencasts

Re: restful route for blog

thanks ryan, i was trying something similar but it was 2am and i had my stupid goggles on. i was putting the same route, but trying to nest it within the blog resource and it wasn't loving it.

Re: restful route for blog

out of interest, ryan, how would this be achieved as a resource route? would it be better as a resource route or is a map route fine?

Re: restful route for blog

I think this works better as a map.archives route. You can extend your map.resources to support this:

map.resources :blogs, :collection => { :archives => :get }

But here (AFAIK) you can't control the URL of that archives aspect. Doing map.archives manually in the routes I think is the best (and only?) approach.

Railscasts - Free Ruby on Rails Screencasts

Re: restful route for blog

thanks ryan. just like to make sure i'm getting it right.. smile