Merb 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.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    1 Favorite

    Merb Router - Presentation Transcript

    1. Bryan Ray http://bryanray.net
    2. The Merb Router In Depth ... Code Repository https://github.com/merbday/atlanta http://github.com/merbday/atlanta/tree/master/router
    3. Thanks Carl Lerche Anyone else who has contributed to Merb ...
    4. Simple Routes Merb::Router.prepare do # resources :articles # RESTful routes # resources :posts end
    5. Match 3 simple route Merb::Router.prepare do match(\"/signup\"). to(:controller => \"users\", :action => \"new\"). name(:signup) end Generating the route: url(:signup) => /signup
    6. Routes with variables Merb::Router.prepare do match(\"/articles/:year/:month/:day/:title\"). to(:controller => \"articles\", :action => \"show\"). name(:article) end
    7. Routes with variables Generating url(:article, :year => 2008, :month => 07, :day => 22, :title => \"Awesomeness\") # => /articles/2008/07/22/Awesomeness
    8. Optional Path Segments match('/articles(/:year(/:month(/:day)))/:title'). to( :controller => :articles, :action => \"with_optional_segments\"). name(:articles) /articles/hello # => { :title => “hello” } /articles/2008/12/hello # => { :title => “hello”, :year => “2008”, :month => “12” }
    9. Routes with conditions match(\"/articles\") do with(:controller => \"articles\") do match(\"/:title\", :title => /[a-z]+$/). to(:action => \"with_title\") match(\"/:id\", :id => /^[\\d]+$/).to(:action => \"with_id\") end end /articles/hello # => Renders the #with_title action /articles/12 # => Renders the #with_id action
    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. The Subdomain Problem module Merb class Request def account subdomains(0).first end end end
    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. Default Routes def default_routes(params = {}, &block) match(\"/:controller(/:action(/:id))(.:format)\"). to(params, &block).name(:default) end
    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. Nested Routes resources :articles resources :comments end Generating link_to \"Article Comments\", url(:article_comments, 1) # => /articles/1/comments
    16. Multiple Nested Routes resources :articles do resources :comments end resources :songs do resources :comments end resources :photos do resources :comments end
    17. Generating the routes... 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)
    18. Yea, right ... link_to \"View Comment\", resource(comment.commentable, comment) Much cleaner ...
    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. Friendly URLs Route resources :users, :identify => :name Generating resource(@user) # => /users/bryan
    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. Redirecting legacy URLs Legacy URL /articles/123-hello-world New URL /articles/hello-world
    23. In the Controller? match(\"/articles/:id\"). to(:controller => \"articles\", :action => \"show\")
    24. Use regex to match? match(\"/articles/:id\", :id => /^\\d+-/). to(:controller => \"articles\", :action => \"legacy\") match(\"/articles/:url\"). to(:controller => \"articles\", :action => \"show\")
    25. 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
    26. Checking authentication match(\"/secret\").defer_to do |request, params| if request.session.authenticated? params end end
    27. Thanks!
    28. Thanks!

    + Bryan RayBryan Ray, 11 months ago

    custom

    1391 views, 1 favs, 0 embeds more stats

    Presentation on the merb router at MerbDay Atlanta. more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1391
      • 1391 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 1
    • Downloads 11
    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