FIXING NAMESPACE NOT FOUND ERROR ON MERB
September 26, 2008
If you have just completed an application, its more than likely you would like to have an admin portal to administer users. The easiest way to do this in Merb is to use the ‘merb-gen resource_controller’ command in your application directory to generate a CRUD controller and views for the resource. This is similar to using scaffold in rails.
Say for instance I have a resource called Posts and I want to generate an admin front-end to it. All I need to do is change to the application directory and type:
merb-gen resource_controller admin/posts
This creates a subfolder called ‘admin’ within your controllers and a new posts controller within that to handle all the CRUD functions. However, on MERB 0.9.5, the controller is missing the module keyword and if you try to run the above you would receive a namespace or controller not found error.
To fix this, open up ‘admin/posts’ controller and add ‘module Admin’ to the file:
module Admin
class Posts < Application
....
end
end
Next add the required namespace to the ‘config/router.rb’ file:
Merb::Router.prepare do |r|
r.namespace :admin do |admin|
admin.resources :posts
end
end
Fire up your app and it should all work. I’m not certain if this is fixed in subsequent versions of Merb but I did notice a ticket has been submitted for nested namespaces error. If anyone reading this has more information on the official line, please do leave a comment
NEWS FLASH:
Just received an up-to-date comment from one of the MERB framework developers who read this post and confirmed that the latest version of ‘merb-gen’ has been fixed to prevent the error above. So upgrade your version of Merb to prevent it. And you still have to add in the namespace in your router file manually.
September 26, 2008 at 6:37 pm
Hi,
We did fix that problem and you won’t see this error with the latest version of merb-gen.
However, please note that the generator does NOT append your routes with the namespaced routes. You still have to do that manually.
-Matt