我想要的是证明我做的事情是对的。我的Ruby on Rails应用程序采用以下结构:
/home
about.rhtml
index.rhtml
/display
index.rhtml
/data <--这由jQuery从display\索引页面调用,以提供要呈现的数据
push.js.erb
pull.js.erb
/layout
home.rhtml
display.rhtml
一切正常,但我现在想添加一个针对移动设备的站点。虽然iPhone正确地呈现了网站,但如果能提供更有针对性的体验就更好了。理想情况下,我正在考虑通过.htaccess重定向到一个iPhone.domain.com。
为此,我在考虑为每个设备添加另一个视图
/iPhone
home.rhtml
about.rhtml
display.rhtml
但是,感觉上有很多数据是重复的,例如about页面会在两个地方。我想我可以有一个partial,然后做一些像render :partial => 'home/about‘这样的事情,但这似乎有点老生常谈。
我如何开发我的网站来支持这一点呢?
我正在考虑这样的结构,但又不确定如何组织代码-我该如何告诉它在iPhone目录中呈现视图...同时不应用主布局
/display
/iphone
index.rhtml
我真的想要一些建议,以最好的方式来接近这一点和结构的应用程序。虽然应用程序目前遵循一种结构,但它们可能会在不同的方向上运行。
谢谢
本
发布于 2010-01-21 01:56:58
Iphone实际上在没有任何特殊格式的情况下,在渲染网页方面做得很好。
然而,在我的Android手机上,浮动内容似乎被切断了,因此需要为该手机提供自定义视图。为此,您需要创建一个不同的布局(例如mobile_application.html.erb),并在您的application_controller中添加以下内容:
layout :select_layout
def select_layout
session.inspect # force session load
if session.has_key? "layout"
return (session["layout"] == "mobile") ? "mobile_application" : "application"
end
return detect_browser
end
def detect_browser
agent = request.headers["HTTP_USER_AGENT"].downcase
MOBILE_BROWSERS.each do |m|
return "mobile_application" if agent.match(m)
end
return "application"
end
其中,MOBILE_BROWSERS是要作为移动设备进行匹配的用户代理字符串数组。
我在这里写了一篇关于这个的博客:
http://www.arctickiwi.com/blog/2-mobile-enable-your-ruby-on-rails-site-for-small-screens
干杯
发布于 2009-08-16 13:59:17
首先,您应该使用.html.erb作为模板扩展
其次,您可以根据用户代理(request.user_agent
)使用逻辑来检测要使用的布局类型。
layout :site_layout
def site_layout
some_way_to_detect_the_layout_to_use
end
请注意,user_agent是可以伪造的,但大多数人不会费心去伪造它,所以对于99.9%的情况,解决方案应该是“足够好的”。
https://stackoverflow.com/questions/1284169
复制相似问题