百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

mobylette

> 编程语言
开源

Ruby on Rails 的移动请求处理

303 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Ruby on Rails 的移动请求处理

http://tscolari.github.com/mobylette/mobylette_images/mobylette.jpg

Mobylette 2.0+ deprecated the respond_to_mobile_requests method. Now you must use include Mobylette::RespondToMobileRequests instead. to configure it, see the configuration. Also stylesheet and javascript helpers were removed.

Mobylette 1.6+ only supports Ruby 1.9.2+ For Ruby 1.8.7 support, please use version [:mobile, :html]. You can override this and add your own fall back chains using the mobylette_config`.

mobylette_config do |config| config[:fallback_chains] = { mobile: [:mobile, :html], iphone: [:iphone, :mobile, :html], ... } end

When you create a custom format with fall back chains, :iphone for example, you must register it as a Mime::Type:

config/initializers/mime_types.rb

Mime::Type.register_alias 'text/html', :iphone

this is very important, don't forget!

:mobile is already registered!

If you don't want any fall backs, just set it to:

mobylette_config do |config| config[:fallback_chains] = { mobile: [:mobile] } end

=== XHR Requests

By default the mobile device verification will skip XHR requests, and these will be served as if mobylette wasn't there. You can override this behavior by setting the :skip_xhr_requests option to false on your controller:

mobylette_config do |config|
  config[:skip_xhr_requests] = false
end

You may need to use this if you are using JQuery mobile or something similar in your application.

=== Registering Mobile Devices

Mobylette 3.0+ has a request_device? helper. By default only :iphone, :ipad, :ios and :android devices come registered. But you can register any device using the mobylette_config method:

mobylette_config do |config| config[:devices] = { my_unique_phone: %r{UniquePhone 1.2.3}, ... } end

Note: This will not add the device to the mobile user_agent detection. For that read #Custom User Agents.

== Skipping mobile filter

In the case you need to skip a mobile_request for been treated as mobile, you can pass the skip_mobile=true param to the url/request.

For example, you are using jquery_mobile and by that :skip_xhr_requests = false, but there is a special case where you need to process an Ajax, then you can use this param.

== Forcing/Ignoring Mobile Requests

You may force your user to aways render the mobile format, or to aways render the default request format (when the request comes from a mobile device). You can use the session var :mobylette_override for doing it:

session[:mobylette_override] = :ignore_mobile

This will skip the code that would force the mobile format. By doing this, your user will aways render the 'original' version of your app.

session[:mobylette_override] = :force_mobile

This will force the mobile format rendering, no matter from where the user is requesting it (unless it's a xhr request).

session[:mobylette_override] = nil

=== Notice: Be sure you are forcing / skiping mobile requests e.g. in a before_filter like : skip_or_force_mobile BEFORE you are including the Mobylette::RespondToMobileRequests module. Mobylette adds own before_filter to check the session var which would be executed before your filter.

class ApplicationController :controller end

This will add 3 methods to your test scope:

force_mobile_request_agent(agent = 'Android')

This will force a mobile user_agent, allowing you to test mobile requests.

reset_test_request_agent

This will reset your user_agent to the test default "Rails Testing". You don't need to call this every time, all your requests by default are "Rails Testing" in your test env.

set_session_override(override_value)

This will force the session_override value in the users session. Values possible values are: :ignore_mobile and :force_mobile

Friendly note: on your tests, call these functions BEFORE you make the request, otherwise they are useless =p

= License

MIT License. Copyright 2012 Tiago Scolari.

Issues· 26 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言