I have a blog function in my rails app. Currently I only have 4 bloggers on the site and have a (pretty unsatisfied) way of routing them like this:
map.scarlet '/scarlet.:format', :controller => 'blogs', :action => 'show', :id => 'scarlet'
map.alba '/andreasweinas.:format', :controller => 'blogs', :action => 'show', :id => 'alba'
So that they are nicely accessible by typing: mysuperwebsite.com/scarlet
Now I would like to provide archives for the blogs accessible like so:
mysuperwebsite.com/scarlet/2009 - shows all entries from 2009
mysuperwebsite.com/scarlet/2009/06 - shows all entries from june 2009
Would anyone recommend how to a) improve my blog routing having in mind that in the future I'll have far more bloggers, and b) how to route for the archives without breaking the paths? I'm thinking an archives controller?
Models:
# Blog.rb
class Blog < ActiveRecord::Base
has_many :entries, :dependent => :destroy
belongs_to :user
end
class Entry < ActiveRecord::Base
named_scope :published, :conditions => ["published_at < ?", Time.zone.now], :order => 'published_at DESC'
belongs_to :blog
end
Blog Controller:
def show
@blog = Blog.find_by_slug(params[:id])
@entries = @blog.entries.published, :order => 'published_at DESC'
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @blog.entries }
format.rss
end
end