Ruby on Rails移动应用程序 [英] Ruby on Rails Mobile Application

查看:76
本文介绍了Ruby on Rails移动应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发Ruby on Rails应用程序,该应用程序将检测客户端,即连接到服务器并呈现适当布局的移动设备(浏览器). 我尝试使用以下链接,但仍无法连接.有什么建议吗?

I am trying to develop a Ruby on Rails application that will detect the client i.e the mobile (browser) that connects to the server and render the appropriate layout. I tried using the following link but still not able to connect it. Any suggestions ?

http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens

我正在使用Opera Mini模拟器来测试应用程序.

I am using Opera Mini Emulator to test the application.

推荐答案

为此,我看到的最优雅的解决方案是做两件事:基于请求中的用户代理识别移动用户并使用自定义的Rails哑剧进行响应,从而允许移动用户使用自定义HTML模板.

The most elegant solution for this I've seen is to do two things: recognize mobile users based on user-agent in the request and use a custom Rails mime type to respond with, allowing custom HTML templates for mobile users.

config/initializers/mime_types.rb中定义只是HTML的自定义移动" Rails MIME类型:

Define a custom 'mobile' Rails MIME type in config/initializers/mime_types.rb that's just HTML:

Mime::Type.register_alias "text/html", :mobile

添加帮助者/过滤器以响应移动用户:

Add a helper/filter to respond to mobile users:

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

在控制器中调度移动设备或常规设备:

Dispatch mobile or regular in controllers:

respond_to do |format|
  format.html { }   # Regular stuff
  format.mobile { } # other stuff
end

这篇关于Ruby on Rails移动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆