How can I add a view path to Rails's partial rendering lookup?

时光毁灭记忆、已成空白 提交于 2019-11-27 21:59:57
twmills

There's already something built into rails that facilitates this type of "theming" for you. It's called prepend_view_path.

http://api.rubyonrails.org/classes/ActionView/ViewPaths/ClassMethods.html#method-i-prepend_view_path

There's also append_view_path for adding paths to the end of the lookup stack.

I have this successfully working in production:

 class ApplicationController < ActionController::Base
   before_filter :prepend_view_paths

   def prepend_view_paths
     prepend_view_path "app/views/#{current_app_code}"
   end
 end

Now every controller will first look in "views/app1" (or whatever your dynamic name turns out to be) for the views corresponding to the action being called.

It's also smart enough to check all the defined paths for the file you're looking for, so it rolls back to the default location if one isn't found.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!