如何更改 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屋!