Arel
Arel

Reputation: 3938

Multiple inheritance in Ruby ... Inheriting from multiple controller classes.

I have a Rails app. All my controllers inherit from a BaseController that inherits from the ApplicationController::Base This is for organization of my partials into separate namespaces. It works great.

Here is the problem.

I am trying to use the Devise gem, but the Devise::SessionsController can't find my partials, which are stored in a folder named Base. My solution was to create a local controller that inherited from Devise::SessionsController like this:

module Account
  class SessionsController < Devise::SessionsController
  end
end

The problem is that the this local SessionsController still needs to inherit from my BaseController so it can find my partials. Make sense?

BaseController code:

module Account
  class BaseController < ApplicationController
  end
end

ApplicationController code:

class ApplicationController < ActionController::Base
  protect_from_forgery

end

I've been reading about mixins, but can't see how I can use mixins to solve this problem, because I don't have any methods to import from the class ...

Upvotes: 1

Views: 920

Answers (1)

Arel
Arel

Reputation: 3938

The solution to this is simply to make the path to your partials more explicit. So in my example, in my Application layout file I changed the partial path from this:

<%= render "menu" %>

to this:

<%= render "account/base/menu" %>

This works because rails looks for the partial in the regular places, but can't find it and throws an error. Specifying exactly where the partial can be found when putting the partial into your HTML solves the problem of Rails or any gems from not knowing where your partials are.

Upvotes: 0

Related Questions