在 Ruby on Rails 中支持移动设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1160331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:30:12  来源:igfitidea点击:

Supporting Mobile Devices in Ruby on Rails

ruby-on-railsrubyruby-on-rails-pluginsmobile-website

提问by Eran Kampf

What is the best way to develop a rails application that has special views for different mobile devices? Basically I'm looking for something like this ASP.NET MVC solution: http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

开发对不同移动设备具有特殊视图的 rails 应用程序的最佳方法是什么?基本上我正在寻找这样的 ASP.NET MVC 解决方案:http: //www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx

采纳答案by Michael Glenn

Two articles that use the iPhone as an example of serving up a different view based on a mobile sub-domain or a by detecting the user-agent. Essentially you'll create a different view using something like viewname.iphone.erb or viewname.mobile.erb and set the request.format variable to iphone or mobile respectively.

两篇文章以 iPhone 为例,基于移动子域或通过检测用户代理提供不同的视图。本质上,您将使用 viewname.iphone.erb 或 viewname.mobile.erb 之类的内容创建不同的视图,并将 request.format 变量分别设置为 iphone 或 mobile。

iPhone subdomains with Rails

带有 Rails 的 iPhone 子域

iPhone on Rails - Creating an iPhone optimised version of your Rails site using iUI and Rails 2

iPhone on Rails - 使用 iUI 和 Rails 2 创建 iPhone 优化版本的 Rails 站点

回答by ideasasylum

You might be looking for mobile_fu. It's a plugin that automatically changes the Rails format from :html to :mobile for Nokia, Blackberry, iPhone and Palm users. You can then just provide an {action}.mobile.erbto complement {action}.html.erband it will render the mobile view. If you need something more fine-grained, it allows you to do:

您可能正在寻找mobile_fu。它是一个插件,可以为诺基亚、黑莓、iPhone 和 Palm 用户自动将 Rails 格式从 :html 更改为 :mobile。然后您可以只提供一个{action}.mobile.erb来补充{action}.html.erb,它将呈现移动视图。如果您需要更细粒度的东西,它允许您执行以下操作:

is_mobile_device?

and there's various other helper methods

还有其他各种辅助方法

回答by Gowiem

I found this railscast extremely helpful for this situation: http://railscasts.com/episodes/199-mobile-devices

我发现这个 railscast 对这种情况非常有帮助:http://railscasts.com/episodes/199-mobile-devices

Hope it helps somebody out.

希望它可以帮助某人。

回答by Eric Bock

Ernie Miller wrote a postabout how to provide mobile templates for your views. What's nice is that his approach doesn't force you to provide a mobile template if it isn't required. Instead, rails will fall back on your main template if the mobile version isn't found.

厄尼·米勒写了一个帖子关于如何为你的观点提供移动模板。不错的是,如果不需要,他的方法不会强迫您提供移动模板。相反,如果找不到移动版本,rails 将退回到您的主模板。

回答by DavidG

I just saw a really illuminating video on this: http://www.engineyard.com/video/12678746. There is a lot more to a decent mobile web experience then directly porting pages.

我刚刚看到了一个非常有启发性的视频:http: //www.engineyard.com/video/12678746。与直接移植页面相比,体面的移动网络体验还有很多。

回答by Ji?í Stránsky

You might want to try Mobvious - https://github.com/jistr/mobvious

您可能想尝试 Mobvious - https://github.com/jistr/mobvious

Detects mobiles vs. desktops + tablets, or if you want, detects mobile vs. tablets vs. desktops.

检测手机与台式机 + 平板电脑,或者如果需要,检测手机与平板电脑与台式机。

It also has support for manual selection by users (overriding the detection) if you want to allow users to manually switch interface versions.

如果您希望允许用户手动切换界面版本,它还支持用户手动选择(覆盖检测)。

And there is an extension mobvious-railsthat adds some helpers to your controllers, views and CoffeeScript.

还有一个扩展mobvious-rails可以为你的控制器、视图和 CoffeeScript 添加一些帮助程序。

回答by Jonathon Horsman

I recently wrote an article about this which you might find useful:

我最近写了一篇关于这个的文章,你可能会觉得有用:

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

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

Hope that helps

希望有帮助

Jonno

中野