Search code examples
ruby-on-railsrubyruby-on-rails-3.2controllersnomethoderror

Rails 3.2.3 namespaced controllers being overridden by global controllers with same name


When the global application controller is loaded first, the namespaced application controller does not load when loading pages within that namespace. The application controller looks like this:

class ApplicationController < ActionController::Base
 protect_from_forgery
end

And the namespaced application controller looks like this:

class Admin::ApplicationController < ApplicationController

def authenticate_admin!
 if current_admin.nil?
  redirect_to new_admin_session_url
 end
end

private

 def current_admin
  @current_admin ||= Admin.find(session[:admin_id]) if session[:admin_id]
 end

helper_method :current_admin
end

When we use the before_filter "authenticate_admin!" like this:

class Admin::AssetsController < Admin::ApplicationController
  before_filter :authenticate_admin!
end

A "NoMethodError in Admin::AssetsController#new" is thrown. This only occurs when we hit the global route before the namespaced route. If the server is restarted and the namespaced route is loaded first everything works properly.


Solution

  • This is happening because you also happen to have an Admin model (a Class) with the same name as your namespace.

    This Google group thread provides a good explanation of what exactly is happening.

    To fix, I would either rename the model to AdminUser or if that is not a possibility, renaming the namespace will also fix the issue.