移动 mime 类型可以回退到“html"吗?在 Rails 中? [英] Can a mobile mime type fall back to "html" in Rails?

查看:14
本文介绍了移动 mime 类型可以回退到“html"吗?在 Rails 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ApplicationController 中使用此代码(取自 此处)检测 iPhone、iPod Touch 和 iPad 请求:

I'm using this code (taken from here) in ApplicationController to detect iPhone, iPod Touch and iPad requests:

before_filter :detect_mobile_request, :detect_tablet_request

protected

def detect_mobile_request
  request.format = :mobile if mobile_request?
end

def mobile_request?
  #request.subdomains.first == 'm'
  request.user_agent =~ /iPhone/ || request.user_agent =~ /iPod/
end

def detect_tablet_request
  request.format = :tablet if tablet_request?
end

def tablet_request?
  #request.subdomains.first == 't'
  request.user_agent =~ /iPad/
end

这允许我拥有像 show.html.erb、show.mobile.erb 和 show.tablet.erb 这样的模板,这很棒,但有一个问题:似乎我必须为每种 mime 类型定义每个模板.例如,在没有定义 show.mobile.erb 的情况下从 iPhone 请求show"动作,即使定义了 show.html.erb 也会抛出错误.如果缺少手机或平板电脑模板,我只想使用 html 模板.这似乎并不太牵强,因为在 mime_types.rb 中,mobile"被定义为text/html"的别名.

This allows me to have templates like show.html.erb, show.mobile.erb, and show.tablet.erb, which is great, but there's a problem: It seems I must define every template for each mime type. For example, requesting the "show" action from an iPhone without defining show.mobile.erb will throw an error even if show.html.erb is defined. If a mobile or tablet template is missing, I'd like to simply fall back on the html one. It doesn't seem too far fetched since "mobile" is defined as an alias to "text/html" in mime_types.rb.

那么,几个问题:

  1. 我做错了吗?或者,有没有更好的方法来做到这一点?
  2. 如果没有,如果手机或平板电脑文件不存在,我可以让手机和平板电脑 mime 类型回退到 html 吗?

如果重要的话,我使用的是 Rails 3.0.1.提前感谢您的指点.

If it matters, I'm using Rails 3.0.1. Thanks in advance for any pointers.

我忘记提及的事情:我最终将转移到单独的子域(正如您在我的示例中所看到的注释),因此模板加载确实需要自动发生,无论如何其中 before_filter 已经运行.

Something I forgot to mention: I'll eventually be moving to separate sub-domains (as you can see commented out in my example) so the template loading really needs to happen automatically regardless of which before_filter has run.

推荐答案

可能重复 在 rails 3.1 中更改视图格式(提供移动 html 格式,回退到普通 html)

然而,我在这个完全相同的问题上苦苦挣扎,并提出了一个相当优雅的解决方案,完美地满足了我的需求.这是我在另一个帖子中的回答.

However, I struggled with this exact same problem and came up with a fairly elegant solution that met my needs perfectly. Here is my answer from the other post.

我想我已经找到了最好的方法来做到这一点.我正在尝试和你一样的事情,但后来我记得在 rails 3.1 中引入了 模板继承,这正是我们需要的东西才能工作.我真的不能对这个实现给予太多信任,因为它的全部内容都在 Ryan Bates 的 railscasts 链接中列出.

I think I've found the best way to do this. I was attempting the same thing that you were, but then I remembered that in rails 3.1 introduced template inheritance, which is exactly what we need for something like this to work. I really can't take much credit for this implementation as its all laid out there in that railscasts link by Ryan Bates.

所以基本上就是这样.

app/views 中创建一个子目录.我标记了我的mobile.

Create a subdirectory in app/views. I labeled mine mobile.

嵌套所有要覆盖的视图模板,其结构格式与视图目录中的结构格式相同.views/posts/index.html.erb ->视图/移动/帖子/index.html.erb

Nest all view templates you want to override in the same structure format that they would be in the views directory. views/posts/index.html.erb -> views/mobile/posts/index.html.erb

在您的 Application_Controller 中创建一个 before_filter 并为此做一些事情.

Create a before_filter in your Application_Controller and do something to this effect.

 before_filter :prep_mobile
 def is_mobile?
   request.user_agent =~ /Mobile|webOS|iPhone/
 end 
 def prep_mobile
   prepend_view_path "app/views/mobile" if is_mobile?
 end

一旦完成,您的文件在移动设备上将默认为移动视图,如果移动设备不存在,则回退到常规模板.

Once thats done, your files will default to the mobile views if they are on a mobile device and fallback to the regular templates if a mobile one is not present.

这篇关于移动 mime 类型可以回退到“html"吗?在 Rails 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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