routes_for_blog.rb 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #---
  2. # Excerpted from "Agile Web Development with Rails",
  3. # published by The Pragmatic Bookshelf.
  4. # Copyrights apply to this code. It may not be used to create training material,
  5. # courses, books, articles, and the like. Contact us if you are in doubt.
  6. # We make no guarantees that this code is fit for any purpose.
  7. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  8. #---
  9. #---
  10. # Excerpted from "Agile Web Development with Rails, 4rd Ed.",
  11. # published by The Pragmatic Bookshelf.
  12. # Copyrights apply to this code. It may not be used to create training material,
  13. # courses, books, articles, and the like. Contact us if you are in doubt.
  14. # We make no guarantees that this code is fit for any purpose.
  15. # Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
  16. #---
  17. require 'rubygems'
  18. require 'action_controller'
  19. ActionController::Routing.use_controllers! [ "article", "blog" ]
  20. rs = ActionController::Routing::Routes
  21. app = ActionDispatch::Integration::Session.new(nil)
  22. ActionController::Routing::Routes.draw do |map|
  23. # Straight 'http://my.app/blog/' displays the index
  24. map.connect "blog/",
  25. :controller => "blog",
  26. :action => "index"
  27. # Return articles for a year, year/month, or year/month/day
  28. map.connect "blog/:year/:month/:day",
  29. :controller => "blog",
  30. :action => "show_date",
  31. :requirements => { :year => /(19|20)\d\d/,
  32. :month => /[01]?\d/,
  33. :day => /[0-3]?\d/},
  34. :day => nil,
  35. :month => nil
  36. # Show an article identified by an id
  37. map.connect "blog/show/:id",
  38. :controller => "blog",
  39. :action => "show",
  40. :id => /\d+/
  41. # Regular Rails routing for admin stuff
  42. map.connect "blog/:controller/:action/:id"
  43. # Catchall so we can gracefully handle badly formed requests
  44. map.connect "*anything",
  45. :controller => "blog",
  46. :action => "unknown_request"
  47. end
  48. rs.recognize_path "/blog"
  49. rs.recognize_path "/blog/show/123"
  50. rs.recognize_path "/blog/2004"
  51. rs.recognize_path "/blog/2004/12"
  52. rs.recognize_path "/blog/2004/12/25"
  53. rs.recognize_path "/blog/article/edit/123"
  54. rs.recognize_path "/blog/article/show_stats"
  55. rs.recognize_path "/blog/wibble"
  56. rs.recognize_path "/junk"
  57. last_request = rs.recognize_path "/blog/2006/07/28"
  58. rs.generate({:day => 25}, last_request)
  59. rs.generate({:year => 2005}, last_request)
  60. rs.generate({:action => "show" , :id => 123}, last_request)
  61. rs.generate({:year => 2006}, last_request)
  62. rs.generate({:year => 2006, :month => nil}, last_request)
  63. app.url_for :controller => 'blog', :action => 'show_date', :year => 2002
  64. app.url_for :controller => 'blog', :action => 'show_date', :overwrite_params => {:year => "2002" }