开发者

setting the "view-name" of a controller

开发者 https://www.devze.com 2023-02-19 03:44 出处:网络
I would like to centralize similar actions of some controllers and wrote a controller from which the other controllers inherites. This works fine.

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消