Namespaced Resources with Merb
Last weekend there were a few people in #merb having issues with namespaced resources, myself included. The example code in the docs ends up adding an extra ‘namespace’ to the urls, so we ended up with a rather ugly block of code to do the routing. Tonight I decided to take a look again at the issue and found another way that works much better. It is now one much shorter line of code for each resource,
r.resources :bars, :namespace => 'foo'
along with the usual wrapping your controllers in ‘module Namespace’. Full example below.
One catch is views always look in /views/namespace/controller, while the controller itself can be picked up in either /controllers or /controllers/namespace. I like the cleanliness of the subfolders so I’m not going to complain.
Example
/config/router.rb
r.resources :bars, :namespace => 'foo'
/app/controllers/foo/bars.rb
module Foo
class Bars < Application
def index
render
end
end
end
/app/views/foo/bars/index.html.erb
Hello Namespace
Named routes generated for use with url()
new_foo_bar: /foo/bars/new edit_foo_bar: /foo/bars/:id/edit custom_foo_bar: /foo/bars/:action/:id foo_bars: /foo/bars foo_bar: /foo/bars/:id
Posted on 01/25/2008 - 0 Comments

Comments