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