Search code examples
ruby-on-railsnested-resources

Using nested controllers


Since my site had an admin section and a normal (front-end user) section, I needed to structure the articles controller in such a way that it was RESTful.

So what I did was , have 2 articles controllers, 1 nested under the admin namespace (which would result in admin/articles) and the other one as a normal articles resource (/articles). (I followed this blog.)

Now I started facing issues such as

A copy of AuditObserver has been removed from the module tree but is still active!

2 questions .

  1. Is this error really because of me using such a structure of nested resources?
  2. Is it a good programming practice to use such a structure? If not, is there a better alternative?

Thanks!


Solution

  • The structure is perfectly fine, and your code will probably function just fine in production mode. The issue usually arises in development when modules or classes are not 'unloaded' after a first request. Without seeing the code it's hard to tell exactly which module or plugin might be causing this issue, but you might want to have a look at this blog post.

    You can usually solve this issue by loading the offending module or class only once:

    config.autoload_once_paths << '/path/to/class/or/module.rb'
    

    or by reloading your plugins in development mode:

    config.reload_plugins = true if Rails.env == 'development'