Merb Pluming - The Router

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

2 comments

Comments 1 - 2 of 2 previous next Post a comment

  • + guest5f5a20a guest5f5a20a 11 months ago
    the match(:subdomain ... thing should actually be match(:subdomains => /(.+)/)
  • + maraby Matt Todd 2 years ago
    Hey man, was cool seeing a good presentation on the routing capabilities... not enough documentation exists about it especially since the rewrite and this is a great step in spreading this knowledge.
Post a comment
Embed Video
Edit your comment Cancel

7 Favorites

Merb Pluming - The Router - Presentation Transcript

  1. Merb Plumbing The Router
  2. Who am I?
  3. Some simple routes Matching Merb::Router.prepare do match(\"/signup\").to( :controller => \"users\", :action => \"new\" ).name(:signup) end Generating url(:signup) => /signup
  4. Routes with variables Matching match(\"/articles/:year/:month/:day/:title\").to( :controller => \"articles\", :action => \"show\" ).name(:article) Generating url(:article, :year => 2008, :month => 07, :day => 22, :title => \"Awesomeness\") /articles/2008/07/22/Awesomeness
  5. Routes with conditions Matching match(\"/articles\") do with(:controller => \"articles\") do match(\"/:title\", :title => /[a-z]+/). to(:action => \"with_title\") match(\"/:id\").to(:action => \"with_id\") end end /articles/Hello_world => with_title /articles/123 => with_id
  6. More conditions Matching Merb::Router.prepare do match(:domain => \"blogofawesomeness.com\") do match(\"/the-daily-awesome\", :method => \"get\"). to(:articles) # Any other route... end end
  7. The subdomain problem match(:subdomain => /(.*)/). to(:account => \":subdomain[1]\") do # application routes here... end
  8. Optional path segments Matching match(\"/articles(/:year(/:month(/:day)))/:title\"). to(:controller => \"articles\", :action => \"show\"). name(:article) Generating /articles/Hello => { :title => “Hello” } /articles/2008/07/Hello => { :year => 2008, :month => “07” :title => “Hello” }
  9. ... and generation still works! url(:article, :title => \"Hello\") url(:article, :year => \"2008\", :title => \"Hello\") url(:article, :year => \"2008\", :month => \"07\", :title => \"Hello\") url(:article, :year => \"2008\", :month => \"07\", :day => \"22\", :title => \"Hello\")
  10. Getting Fancy Matching match(\"(:pref)/:file(.:format)\", :pref => \".*\"). to(:controller => \"cms\").name(:page) /hi => { :file => “hi” } /hi.xml => { :file => “hi”, :format => “xml”} /path/to/hi => { :pref => “/path/to”, :file => “hi }
  11. And you can still generate it url(:page, :file => \"hi\") /hi url(:page, :pref => \"/path/to\", :file => \"hi\") /path/to/hi url(:page, :pref => \"/hello\", :file => \"world\", :format => \"mp3\") /hello/world.mp3
  12. The default route def default_route(params = {}, &block) match(\"/:controller(/:action(/:id))(.:format)\"). to(params, &block).name(:default) end
  13. AWESOME!
  14. Resource routes Matching resources :articles /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. and you can nest them Matching resources :articles do resources :comments end Generating link_to \"view comment\", url(:article_comment, comment.article_id, comment.id)
  16. Nested multiple times resources :articles do resources :comments end resources :songs do resources :comments end resources :photos do resources :comments end
  17. and... generating? link_to \"view comment\", 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) wha...?
  18. Nah... how about this? link_to \"view comment\", resource(comment.commentable, comment)
  19. Some more examples resources :users do resources :comments end Generating resource(@user) resource(@user, :comments) vs. vs. url(:user, @user) url(:user_comments, @user) resource @user, @comment vs. url(:user_comment, @user, @comment)
  20. Oh yeah... match(\"/:project\") do resources :tasks end - While currently at /awesome/tasks resource(@task) => /awesome/tasks/123 resource(@task, :project => \"woot\") => /woot/tasks/123
  21. AWESOME!
  22. Friendly URLs Matching resources :users, :identify => :name Generating resource(@user) # => /users/carl
  23. It works as a block too Matching identify(Article=>:permalink, User=>:login) do resources :users do resources :articles end end Generating resource(@user, @article) /users/carl/articles/hello-world
  24. AWESOME!
  25. Redirecting legacy URLs Old: /articles/123-hello-world New: /articles/Hello_world
  26. Handle it in the controller? match(\"/articles/:id\"). to(:controller => \"articles\", :action => \"show\")
  27. Try to do it with Regexps? match(\"/articles/:id\", :id => /^\\d+-/). to(:controller => \"articles\", :action => \"legacy\") match(\"/articles/:url\"). to(:controller => \"articles\", :action => \"show\")
  28. Or, add your own logic! match(\"/articles/:url\").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
  29. Authentication works too match(\"/secret\").defer_to do |request, params| if request.session.authenticated? params end end
  30. Works great for plugins def protect(&block) protection = Proc.new do |request, params| if request.session.authenticated? params else redirect \"/login\" end end defer(protection, &block) end
  31. Awesome! protect.match(\"/admin\") do # ... admin routes here end
  32. Thank you (Awesome!)

+ carllerchecarllerche, 2 years ago

custom

4194 views, 7 favs, 0 embeds more stats

More info about this document

© All Rights Reserved

Go to text version

  • Total Views 4194
    • 4194 on SlideShare
    • 0 from embeds
  • Comments 2
  • Favorites 7
  • Downloads 87
Most viewed embeds

more

All embeds

less

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

Cancel
File a copyright complaint
Having problems? Go to our helpdesk?

Categories