设置“视图名称”一个控制器的
我想集中一些控制器的类似操作,并编写一个其他控制器继承的控制器。这很好用。
# 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
但现在我想将视图集中到“基本”控制器的视图,因此如果调用继承控制器,则使用的controller_path应该是基本控制器的。
有没有办法重写控制器名称或控制器路径?
我查看了 AbstractController::Base 的源代码,发现(第 90 行)
def controller_path
@controller_path ||= name.sub(/Controller$/, '').underscore unless anonymous?
end
所以我只需要从我的基本控制器设置 @controller_path 不是吗?这不会改变任何东西:
#just does the same as above
class EditorController < ApplicationController
@controller_path = 'editor'
def index
render 'index'
end
end
那么有没有办法手动设置controller_path?
非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该死的是我自己发现的!
我刚刚覆盖了controller_path方法:
这将永远使用任何继承控制器的视图文件夹“编辑器”。
damn I found it on my own!
I just overwrote the controller_path method:
this will ever use the view-folder 'editor' for any inheriting controller.