我正在尝试开发一个 Ruby on Rails 应用程序,它将检测客户端,即连接到服务器并呈现适当布局的移动设备(浏览器)。
我尝试使用以下链接,但仍然无法连接。有什么建议么 ?
http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens
我正在使用 Opera Mini Emulator 来测试应用程序。
最佳答案
我见过的最优雅的解决方案是做两件事:根据请求中的用户代理识别移动用户,并使用自定义 Rails mime 类型进行响应,允许为移动用户自定义 HTML 模板。
在 config/initializers/mime_types.rb
中定义一个自定义的“移动”Rails MIME 类型,它只是 HTML:
Mime::Type.register_alias "text/html", :mobile
添加助手/过滤器以响应移动用户:
def mobile_user_agent?
@mobile_user_agent ||= ( request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/] )
end
然后..
before_filter :handle_mobile
def handle_mobile
request.format = :mobile if mobile_user_agent?
end
制作自定义移动模板:
app/views/users/show.html.erb => app/views/users/show.mobile.erb
在 Controller 中调度移动或常规:
respond_to do |format|
format.html { } # Regular stuff
format.mobile { } # other stuff
end
关于ruby-on-rails - Ruby on Rails 移动应用程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5126085/