Search code examples
ruby-on-rails-3controlleractionpack

setting the "view-name" of a controller


I would like to centralize similar actions of some controllers and wrote a controller from which the other controllers inherites. This works fine.

# calling Configurations#index will render configurations/index.html.erb
# while 'configurations' being the internal controller_path used to look for the view
class ConfigurationsController < EditorController
end

class EditorController < ApplicationController
 def index
  render 'index'
 end
end

But now I would like to centralise the views to the "base"-controller one's, so if an inheriting controller is called, the controller_path used should be the base-controller one's.

Is there a way, to rewrite a controllers name or controller_path?

I looked at the source of AbstractController::Base and found that (line 90)

def controller_path
  @controller_path ||= name.sub(/Controller$/, '').underscore unless anonymous?
end

So I just need to set @controller_path from my base-controller don't I ? This doesn't change anything:

#just does the same as above
class EditorController < ApplicationController
 @controller_path = 'editor'
 def index
  render 'index'
 end
end

So is there a way to set the controller_path manually?

great thanks in advance!


Solution

  • damn I found it on my own!

    I just overwrote the controller_path method:

    class EditorController < ApplicationController
     def controller_path
      'editor'
     end
     #...
    end
    

    this will ever use the view-folder 'editor' for any inheriting controller.