The document discusses routing in Merb, including:
- Setting up simple routes that map URLs to controllers and actions
- Defining routes that accept variable path segments
- Adding conditions and optional path segments to routes
- Handling subdomain routing
- Generating URLs from routes
- Creating default, resourceful, and nested routes
- Using friendly URLs and redirecting legacy URLs
- Checking authentication in routes
6. 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
7. Routes with variables
Merb::Router.prepare do
match(quot;/articles/:year/:month/:day/:titlequot;).
to(:controller => quot;articlesquot;, :action => quot;showquot;).
name(:article)
end
10. 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
11. 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')
15. 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
17. Multiple Nested Routes
resources :articles do
resources :comments
end
resources :songs do
resources :comments
end
resources :photos do
resources :comments
end
19. Yea, right ...
link_to quot;View Commentquot;, resource(comment.commentable, comment)
Much cleaner ...
20. 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)
22. 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