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

查看:72
本文介绍了移动哑剧类型可以回退到"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.html.erb,但从iPhone请求"show"操作而未定义show.mobile.erb也会引发错误.如果缺少手机或平板电脑模板,我想简单地使用html模板.由于移动"在mime_types.rb中被定义为"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.

所以基本上就是这样.

So this is basically how it goes.

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

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

嵌套所有要覆盖的视图模板,它们的结构格式应与它们在views目录中的结构格式相同. views/posts/index.html.erb -> views/mobile/posts/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.

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

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