bod*_*ous 4 model-view-controller rendering ruby-on-rails ruby-on-rails-3
我有一个应用程序,提供多个网站.与Stack Exchange类似,这几个站点的行为非常相似.
给出以下视图目录结构:
views/
shared/users/index.html.erb
app1/users/index.html.erb
app2/users/
Run Code Online (Sandbox Code Playgroud)
如何在Rails中重写默认模板渲染
提前致谢
我知道你已经接受了这个答案,但我认为你不需要创建自己的模板解析器.
如果我正确理解您的问题,您会尝试根据应用当前状态的某些方面"主题化"您的观点.我以前使用这个花花公子的小控制器方法做了同样的事情:
prepend_view_path "app/views/#{current_app_code}"
Run Code Online (Sandbox Code Playgroud)
将其放在应用程序控制器中的before_filter中,并且所有控制器都将遵守:
class ApplicationController < ActionController::Base
before_filter :prepend_view_paths
def prepend_view_paths
prepend_view_path "app/views/#{current_app_code}"
end
end
Run Code Online (Sandbox Code Playgroud)
现在,如果"app1"是当前应用程序,则当请求"/ users"时,rails将首先查找"app/views/app1/users/index.html.erb".
如果在那里找不到它,它将回滚到"app/views/users/index.html.erb"的默认位置.
希望这能为您提供另一种选择.