Merb Router

2,534 views

Published on

Presentation on the merb router at MerbDay Atlanta.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,534
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
19
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Merb Router

  1. 1. Bryan Ray http://bryanray.net
  2. 2. The Merb Router In Depth ... Code Repository https://github.com/merbday/atlanta http://github.com/merbday/atlanta/tree/master/router
  3. 3. Thanks Carl Lerche Anyone else who has contributed to Merb ...
  4. 4. Simple Routes Merb::Router.prepare do # resources :articles # RESTful routes # resources :posts end
  5. 5. Match 3 simple route Merb::Router.prepare do match(quot;/signupquot;). to(:controller => quot;usersquot;, :action => quot;newquot;). name(:signup) end Generating the route: url(:signup) => /signup
  6. 6. Routes with variables Merb::Router.prepare do match(quot;/articles/:year/:month/:day/:titlequot;). to(:controller => quot;articlesquot;, :action => quot;showquot;). name(:article) end
  7. 7. Routes with variables Generating url(:article, :year => 2008, :month => 07, :day => 22, :title => quot;Awesomenessquot;) # => /articles/2008/07/22/Awesomeness
  8. 8. Optional Path Segments match('/articles(/:year(/:month(/:day)))/:title'). to( :controller => :articles, :action => quot;with_optional_segmentsquot;). name(:articles) /articles/hello # => { :title => “hello” } /articles/2008/12/hello # => { :title => “hello”, :year => “2008”, :month => “12” }
  9. 9. Routes with conditions match(quot;/articlesquot;) do with(:controller => quot;articlesquot;) do match(quot;/:titlequot;, :title => /[a-z]+$/). to(:action => quot;with_titlequot;) match(quot;/:idquot;, :id => /^[d]+$/).to(:action => quot;with_idquot;) end end /articles/hello # => Renders the #with_title action /articles/12 # => Renders the #with_id action
  10. 10. The Subdomain Problem The end goal http://<account>.localhost:4000 http://bryan.localhost:4000 Matching inside the router match(:account => 'bryan'). to(:controller => 'articles', :action => 'with_account')
  11. 11. The Subdomain Problem module Merb class Request def account subdomains(0).first end end end
  12. 12. #url generation ... url(:articles, :title => 'hello') # => /articles/hello url(:articles, :year => 2008, :title => 'hello') # => /articles/2008/hello url(:articles, :year => 2008, :month => 12, :title => 'hello') # => /articles/2008/12/hello url(:articles, :year => 2008, :month => 12, :day => 07, :title => 'hello') # => /articles/2008/12/7/hello # Anonymous Parameters url(:articles, 2008, 12, 07, :title => 'hello') # => /articles/2008/12/7/testing
  13. 13. Default Routes def default_routes(params = {}, &block) match(quot;/:controller(/:action(/:id))(.:format)quot;). to(params, &block).name(:default) end
  14. 14. Resource-ful Routes Merb::Router.prepare do # Resource-ful Routes resources :articles end /articles GET => index /articles POST => create /articles/new GET => new /articles/:id GET => show /articles/:id PUT => update /articles/:id DELETE => destroy /articles/:id/edit GET => edit /articles/:id/delete GET => delete
  15. 15. Nested Routes resources :articles resources :comments end Generating link_to quot;Article Commentsquot;, url(:article_comments, 1) # => /articles/1/comments
  16. 16. Multiple Nested Routes resources :articles do resources :comments end resources :songs do resources :comments end resources :photos do resources :comments end
  17. 17. Generating the routes... link_to quot;view commentquot;, comment.commentable.is_a?(Article) ? url(:article_comment, comment.commentable.id, comment.id) : comment.commentable.is_a?(Song) ? url(:song_comment, comment.commentable.id, comment.id) : url(:photo_comment, comment.commentable.id, comment.id)
  18. 18. Yea, right ... link_to quot;View Commentquot;, resource(comment.commentable, comment) Much cleaner ...
  19. 19. More Resource examples resource(@user) vs. url(:user, user) resource(@user, :comments) vs. url(:user_comments, user) resource(@user, @comment) vs. url(:user_comment, user, comment)
  20. 20. Friendly URLs Route resources :users, :identify => :name Generating resource(@user) # => /users/bryan
  21. 21. Can also use a block identify(Article=>:permalink, User=>:login) do resources :users do resources :articles end end Generating resource(@user, @article) # => /users/bryan/articles/hello-world
  22. 22. Redirecting legacy URLs Legacy URL /articles/123-hello-world New URL /articles/hello-world
  23. 23. In the Controller? match(quot;/articles/:idquot;). to(:controller => quot;articlesquot;, :action => quot;showquot;)
  24. 24. Use regex to match? match(quot;/articles/:idquot;, :id => /^d+-/). to(:controller => quot;articlesquot;, :action => quot;legacyquot;) match(quot;/articles/:urlquot;). to(:controller => quot;articlesquot;, :action => quot;showquot;)
  25. 25. Or, add your own logic! match(quot;/articles/:urlquot;).defer_to do |request, params| if article = Article.first(:url => params[:url]) params.merge(:article => article) elsif article = Article.first(:id => params[:url]) redirect url(:article, article.url) else false end end
  26. 26. Checking authentication match(quot;/secretquot;).defer_to do |request, params| if request.session.authenticated? params end end
  27. 27. Thanks!
  28. 28. Thanks!

×