Rails 中不同子域的不同视图
我正在构建一个 Rails 应用程序,我需要对同一应用程序的三个不同视图进行可用性测试。我的想法是根据子域切换默认视图路径。
例如,我希望能够定义类似的路径:
option1.mysite.com => views/option_1
option2.mysite.com => views/option_2
option3.mysite.com => views/option_3
我希望保持模型和控制器相同,但根据子域切换视图。做到这一点的最佳方法是什么?
I am building a Rails application where I need to do a usability test of three different Views for the same application. My thought is to switch out the default view path depending on the subdomain.
For example, I'd like to be able to define the paths something like:
option1.mysite.com => views/option_1
option2.mysite.com => views/option_2
option3.mysite.com => views/option_3
I'd like to keep the Models and Controllers the same, but switch out the Views depending on the subdomain. What might be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们这样做是这样的:
这是我们的应用程序控制器中
set_site
方法的一部分。每个请求都会检查session[:site]
是否已设置;如果没有,它会调用set_site
来设置它。就您的情况而言,现在您只需要在视图中引入逻辑,根据
session[:site]
的值以不同的方式呈现内容,但如果您的实际视图 HTML 相同并且主要区别在于CSS。然后,您只需根据session[:site]
的值加载不同的 CSS 文件。We do it something like this:
That's part of a
set_site
method in our application controller. Every request checks to see ifsession[:site]
is set; if not, it callsset_site
to set it.In your case, now you just need to introduce logic in your views to present things differently depending on the value of
session[:site]
, but it's even better if your actual view HTML is the same and the major difference is in the CSS. Then you just load different CSS files depending on the value ofsession[:site]
.