August 6th, 2009 by Todd Sundsted
It Was an SEO Engineering Nightmare
It was an SEO engineering nightmare made flesh in URLs…
What I needed was something like Merb routing in Rails…
What I found was literally Merb routing in Rails.
It was a perfect fit and an easy win because Merb’s routing just works more like routing should work. In rough benchmarks it was just as fast, too.
I don’t brag on projects very often, but this was a damn nice piece of work. It comes as a Rails plugin. You have your choice of forks on GitHub. I started with drogus because it came Rails 2.3.x ready.
Most existing Rails routes can be rewritten in short order. You need to rewrite them because Rails routes and Merb routes don’t coexist.
To install and use:
- download and install as a plugin
- create config/router.rb
Things you can do (put the following in router.rb):
Merb::Router.prepare do |r|
# yeah yeah
r.match("/posts").
to(:controller => "posts", :action => "index")
# the simple stuff
r.match("/posts/:id").
to(:controller => "posts", :action => "show")
# regex FTW...
# matches "/foo-bar/sys/bang/12-4-6" and gives us:
# {"sys_id"=>"foo-bar", "knob_ids"=>"1246",
# "action"=>"frobnicate", "controller"=>"systems"}
r.match(%r[^/([a-z-]+)/sys/bang/(\d+)-(\d+)-(\d+)$]).
to(:controller => "systems", :action => "frobnicate",
:knob_ids => "[2][3][4]", :sys_id => "[1]")
# matches "/this-is-the-slug/12-3-45" and kindly gives us:
# {"a"=>"12", "b"=>"3", "c"=>"45", "action"=>"show",
# "controller"=>"categories", "id"=>"this-is-the-slug"}
r.match("/:id/:a-:b-:c").
to(:controller => "categories", :action => "show")
# does what you'd expect
r.match("/:controller/:action/:id").
to()
end
Props to the originators. They literally made my day.
Leave a Reply