本文介绍了在 rails 3.1 中更改视图格式(提供移动 html 格式,回退到普通 html)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我们的普通 html 网站旁边创建一个移动网站.使用导轨 3.1.移动站点在子域 m.site.com 中访问.

I'm creating a mobile site next to our normal html site. Using rails 3.1. Mobile site is accessed in subdomain m.site.com.

我已经定义了移动格式(Mime::Type.register_alias "text/html", :mobile).

I have defined mobile format (Mime::Type.register_alias "text/html", :mobile).

在 ApplicationController 中,我有before_filter :mobile_site_before_filter",它可以识别移动站点并根据它设置格式.

In ApplicationController I have "before_filter :mobile_site_before_filter" that recognizes mobile site and sets format according to it.

def mobile_site_before_filter
  if request.subdomains.first == 'm'
    original_format = request.format
    request.format = :mobile
    @mobile_site = true
    request.formats.push(original_format)
  end
end

在布局中,我有main.html.erb"和main.mobile.erb".因此,对于移动网站,使用移动布局.

In layouts I have 'main.html.erb' and 'main.mobile.erb'. So with mobile sites mobile layout is used.

现在一切正常.

在 UserController 中,我有索引操作,它自动选择 index.html.erb 或 index.mobile.erb.无需在移动视图顶部进行额外编码.成功.

In UserController I have index action which chooses index.html.erb or index.mobile.erb automatically. No extra coding in top of mobile-view needed. Success.

但我有很多其他视图,可以使用相同的模板在移动布局内提供服务,但只需稍作更改.

But I have lots of other views where the same template could be used to serve inside mobile layouts but with minor changes.

例如在 MessagesController 中,相同的视图几乎适用于移动设备

E.g. in MessagesController the same view would be almost fine for mobile

In index.html.erb
Normal user info, common for mobile and html
<%= render(:partial => 'messages') %>

渲染messages.html.erb,不需要messages.mobile.erb.移动视图可以用css完成

Rendering messages.html.erb, no need for messages.mobile.erb. mobile view can be done with css

<%# By default self.formats = [:html] because this is .html.erb partial, even in mobile site %>
<%= self.formats = [:mobile, :html] if @mobile_site # How to get rid of this? %>
<%= render(:partial => 'vote_form') %>
<!-- rest of page ... -->

现在我想根据网站呈现vote_form.[mobile|html].erb...

Now i want to render vote_form.[mobile|html].erb depending on the site...

如果我可以在vote_form.html.erb 或vote_form.mobile.erb 之间进行选择,那么现在index.html.erb 部分可以很好地与移动设备一起使用.我可以选择使用移动部分index.html.erb 开头的self.formats = [:mobile, :html] if @mobile_site".但是在所有模板的开头写这个感觉很愚蠢.

Now the index.html.erb partial would be fine to use with mobile if I could just choose between vote_form.html.erb or vote_form.mobile.erb. I can choose to use mobile partial with using"self.formats = [:mobile, :html] if @mobile_site" in the beginning of index.html.erb . But it feels stupid to write this in beginning of all templates.

问题是:

  • self.formats 在哪里出现在视图中(它最初设置的是什么),我如何设置它在移动网站内始终为 [:mobile, :html]?为什么它与我在控制器 before_filter 中设置的不一样?我可以在控制器中以某种方式设置它吗?

(小奖励问题)

  • 这种方法有什么问题吗?大多数情况下使用相同的 html-views,在某些特定情况下使用 mobile.erb 视图代替.为什么这在 Rails 中默认不起作用?

  • Is there something wrong in this approach? Using mostly the same html-views and in some specific cases using mobile.erb views instead. Why doesn't this work by default in rails?

如果发现回退到普通 html 视图,还有其他方法可以选择呈现 :mobile 视图吗?甚至跨部分,以便 html.erb-view 尝试使用 _partial.mobile.erb 部分.

Is there other ways to choose to render :mobile views if found with fallback to normal html view? Even across the partials so that html.erb-view tries to use _partial.mobile.erb partials.

推荐答案

您可以在 mime 类型初始值设定项中为整个应用程序注册新格式:

You can register new format for the whole application in your mime type initializers:

 Mime::Type.register_alias "text/html", :mobile

现在您可以在模板中执行类似操作以指定格式优先级(请参阅 如何在 Rails 中呈现不同格式的一部分?):

Now you can do something like this in your templates to specify format priority(see How do I render a partial of a different format in Rails?):

<% self.formats = [:mobile, :html] %>

在这种情况下,如果有移动模板,它将用于回退到普通 html 模板的渲染.现在您应该只确定用户是否通过移动浏览器浏览并有条件地执行此代码.或者,您可以仅在 ApplicationController 中分配格式值 过滤器,以便自动选择正确的模板.

In this case if there is mobile template it will be used for rendering with fallback to ordinary html template. Now you should only determine if user is browsing via mobile browser and conditionally execute this code. Or you can just assign formats value in ApplicationController filter so correct template will be chosen automaticaly.

更新:

目前似乎还没有使用 Rails 解决这个问题的合法"方法.这是 rails 存储库中的未解决的问题.您可以在那里找到可以解决您问题的补丁,但它使用私有 Rails API,因此可能不稳定.

It seems like by this time there is no "legal" way to solve this problem using Rails. Here is unclosed issue in the rails repository. There you can find patch that can solve your problem, but it uses private Rails API, so it can be unstable.

您也可以尝试实现自己的视图解析器,可能可以解决问题:http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/

Also you can try implement your own view resolver that possibly can solve the problem: http://jkfill.com/2011/03/11/implementing-a-rails-3-view-resolver/

这篇关于在 rails 3.1 中更改视图格式(提供移动 html 格式,回退到普通 html)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 10:35