如何在Rails 3控制器中更改视图文件的默认路径? [英] How to change the default path of view files in a Rails 3 controller?
问题描述
我有一个名为ProjectsController
的控制器.默认情况下,其操作会在app/views/projects
中查找视图.我想为控制器中的所有方法(index
,show
,new
,edit
等...)更改该路径.
I have a controller called ProjectsController
. Its actions, by default, look for views inside app/views/projects
. I'd like to change that path for all methods (index
, show
, new
, edit
etc...) in the controller.
例如:
class ProjectsController < ApplicationController
#I'd like to be able to do something like this
views_path 'views/mycustomfolder'
def index
#some code
end
def show
#some code
end
def new
#some code
end
def edit
#some code
end
end
请注意,我没有使用render
更改每个方法,而是为所有方法定义了默认路径.这可能吗?如果可以,怎么办?
Please note I am not changing each method with render
but defining a default path for all of them. Is this possible? If so, how?
谢谢!
推荐答案
如果没有内置方法,也许您可以为该控制器覆盖render
?
If there's no built-in method for this, perhaps you can override render
for that controller?
class MyController < ApplicationController
# actions ..
private
def render(*args)
options = args.extract_options!
options[:template] = "/mycustomfolder/#{params[:action]}"
super(*(args << options))
end
end
不确定这在实践中的效果如何,或者根本无法实现.
Not sure how well this works out in practice, or if it works at all.
这篇关于如何在Rails 3控制器中更改视图文件的默认路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!