我正在研究为移动应用设计Rails应用程序的方法.
这个想法很常见,使用一组样式用于移动浏览器,另一套用于传统浏览器.
据我所知,在Rails中有两种基本的方法:
使用帮助程序方法检测用户代理,然后执行开关.
application_controller.rb
private
def mobile?
request.user_agent =~ /Mobile|webOS/
end
helper_method :mobile?
Run Code Online (Sandbox Code Playgroud)
application.html.erb
<% unless mobile? %>
<%= stylesheet_link_tag "core" %>
<%else%>
<%= stylesheet_link_tag "mobile" %>
<meta name="viewport" content="width=320; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/>
<% end%>
Run Code Online (Sandbox Code Playgroud)
或者在样式表中使用媒体查询
body {
// basic styles
}
@media all and (max-width: 600px) {
body {
// extra styles for mobile
}
}
@media all and (min-width: 600px) {
body {
// extra styles for desktop
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是什么是权衡?一种方法通常更好还是两者都有良好的用例.
提前致谢