RailsでRESTfulにposts/:year/:month/:dayするにはどうしたらいいんだろう

Rails3で家計簿作ってて、月毎、日毎などの集計ページを作りたい。
blogの例でよくある、 posts/:year/:month/:dayみたいなのの例が、routes.rbのサンプル中にあるだろうと眺めていたらやはりあったけど…

  • routes.rb
  # This is a legacy wild controller route that's not recommended for RESTful applications.
  # Note: This route will make all actions in every controller accessible via GET requests.
  # match ':controller(/:action(/:id(.:format)))'

というわけでRESTfulじゃなくて素敵じゃない模様。

こんなの見つけたけどこれでいいのかなぁ。
/archives/:year/:month/:day routes with REST? - Ruby on Rails - Ruby-Forum

Re: /archives/:year/:month/:day routes with REST?
Posted by Jeff Cohen (jeff) on 2007-05-29 19:34
On May 29, 11:50 am, Douglas Shearer wrote:
> Jeff Emminger wrote:
> > mydomain.com/blog/year/2007/month/5/day/28
>
> That would work, using nested resources. Have decided I was being
> stupid, and have re-implemented my map.connect.

If it's not too late, let me throw in my two cents :-)

REST != map.resources

This is a common perception, because DHH's posts about REST usually
included instructions about map.resources, a feature new to Rails 1.2
that made routing easier *in some cases*.

But you can have a 100% RESTful application just by using map.connect,
too. After all, map.resources is just shorthand for a bunch of named
routes (and named routes are just map.connect statements).

The url part after blog/ identifies your resource, right? So you can
be RESTful:

map.connect /blog/:year:/:month/:day, :controller => :blog, :action
=> :index

Then your BlogController has:

def index
month = params[:month]
year = params[:year]
day = params[:day]

@entries = # get entries here
end

I'm simplifying, but hopefully you get the idea. You can have a
RESTful architecture whether you use map.connect or map.resources.

Jeff
softiesonrails.com

調査中。