Rails 根据不同的域名,选择不同的 views 目录有没有捷径?

tini8 · June 27, 2015 · Last by tini8 replied at June 27, 2015 · 1741 hits

因为要做手机站,访问 m.xxxx.com 的时候想使用手机版的模板,controller、models 手机版倒不用特殊定制了,就是 views 跟 PC 版不一样,有没有办法让访问 m.xxxx.com 的时候加载 views/m/ 文件夹下的模板?

controller 里判断 ua,指定不同的 view

#1 楼 @rubyless 如果在每个 action 里指定 view 是不是有些麻烦,n 个 controller,n 个 action。刚才搜到了 prepend_view_path http://apidock.com/rails/AbstractController/ViewPaths/ClassMethods/prepend_view_path ,不知道管不管用。在 application_controller.rb 里已经通过 self.view_paths.first.to_s 得到了当前的 views 目录

最好的方法是使用:Action Pack variants 吧,具体你可以搜索下

#3 楼 @chunlea 多谢大侠指点迷津,周一我到公司试试

You need to Sign in before reply, if you don't have an account, please Sign up first.