Router Updates for Slices
There’s a bit of confusion about how to use slices like merb-auth with the new router. The once global named routes are now no longer working. Here’s how to get the old behaviour back.
The new router and slices provide a second url method called slice_url this is used like this inside your slice.
slice_url(:MerbAuth, :login)
This protects your application by namespacing the routes. It would also be available via the url method with
url(:merb_auth_login)
This is really good for code since now you can rest assured that a slice isn’t going to clobber your routes.
What about getting back the old global :login named route? Easy. When you add your slice to the router there are now some additional parameters you can use. The relevant ones here are :name_prefix and :path_prefix. Here’s an example of how you’d use them.
Merb::Router.prepare do
slice( :MerbAuth, :name_prefix => nil, :path_prefix => "auth", :default_routes => false )
end
By setting :name_prefix to nil, the routes defined in the slice will not be namespaced. They’ll still be accessible via slice_url inside the slice, but url(:login) will work as well.
The :path_prefix will setup a path prefix on the url for the router, so the above will match routes at ”/auth/login” for example.
:default_routes => false is one that should always be set IMHO. You want your apps default routes, not your slices right? Unless it’s got good reason anyway.
Just a handy quick update to help anyone caught with the new router changes.
Comment On Router Updates for Slices
On September 25, 2008 at 02:13 joel.hansson says:
This is great! Can you update the MerbAuth tutorial too?
Sign in to add your comment