views:

45

answers:

2

Hello,

I am troubleshooting why my ApplicationController's methods don't seem to work in my namespaced admin area and it seems like when I'm in a namespace I cannot access my ApplicationController's private methods, is this right?

If it is like that, what's the best practise to reuse something like Authlogic's example ApplicationController methods in my namespaced controller? I could easily copy and paste the methods to an AdminController or something, and I could also un-private these methods - but this doesn't seem like such a good way to do it.

Here's what the example ApplicationController from Authlogic (and mine) looks like:

class ApplicationController < ActionController::Base
  protect_from_forgery

  helper_method :current_user_session, :current_user

  private
    def current_user_session
      return @current_user_session if defined?(@current_user_session)
      @current_user_session = UserSession.find
    end

    def current_user
      return @current_user if defined?(@current_user)
      @current_user = current_user_session && current_user_session.user
    end  

    def require_user
      unless current_user
        store_location
        flash[:notice] = "You must be logged in to access this page"
        redirect_to new_user_session_url
        return false
      end
    end

    # and some more methods here...

end

And that's how I inherit from it in my namespace:

class Admin::DashboardController < ApplicationController
  layout 'administration'

  require_user # fails

  def index
  end

end

Thanks for your help,

Arne

A: 

I haven't used authlogic, but maybe you need to change

require_user

to

before_filter :require_user
Tim
A: 

I see 2 basic errors:

1) You have declared methods current_user_session, require_user and current_user as private in Application controller. So they can be called only from within this controller but not from its subclasses as Admin::DashboardController is. Explanations you can read in Object Oriented Ruby tutorial.

2) You should use before_filter in Admin::DashboardController:

class Admin::DashboardController < ApplicationController
  layout 'administration'

  before_filter :require_user

  def index
  end
end
Voldy