小编iEu*_*ene的帖子

Rails 3.2友好的URL按日期路由

我想实现具有以下能力的blog \news应用程序:

  1. 显示root的所有帖子: example.com/
  2. 显示所有回答某些年份的帖子: example.com/2012/
  3. 显示所有回答某些年份和月份的帖子: example.com/2012/07/
  4. 按日期和slug显示一些帖子: example.com/2012/07/slug-of-the-post

所以我为routes.rb文件创建了一个模型:

# GET /?page=1
root :to => "posts#index"

match "/posts" => redirect("/")
match "/posts/" => redirect("/")

# Get /posts/2012/?page=1
match "/posts/:year", :to => "posts#index",
  :constraints => { :year => /\d{4}/ }

# Get /posts/2012/07/?page=1
match "/posts/:year/:month", :to => "posts#index",
  :constraints => { :year => /\d{4}/, :month => /\d{1,2}/ }

# Get /posts/2012/07/slug-of-the-post
match "/posts/:year/:month/:slug", :to => "posts#show", :as => :post,
  :constraints => { :year => /\d{4}/, :month => …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails url-routing ruby-on-rails-3

5
推荐指数
1
解决办法
2366
查看次数