Ruby on Rails 视图的移动版本 [英] Mobile version of views for Ruby on Rails

查看:13
本文介绍了Ruby on Rails 视图的移动版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过验证,我正在做正确的事情.我的 Ruby on Rails 应用程序采用以下结构:

I'm after some validation that I'm doing the right thing. I have my Ruby on Rails application in the following structure:

/home
   about.rhtml
   index.rhtml
/显示
   index.rhtml
/data <--这是jQuery从显示索引页面调用以提供要渲染的数据
   push.js.erb
   pull.js.erb
/布局
   home.rhtml
   display.rhtml

/home
   about.rhtml
   index.rhtml
/display
   index.rhtml
/data <--This is called by jQuery from the displayindex page to provide the data to render
   push.js.erb
   pull.js.erb
/layout
   home.rhtml
   display.rhtml

一切正常,但我现在想添加一个针对移动设备的网站.虽然 iPhone 可以正确呈现网站,但提供更有针对性的体验会很好.理想情况下,我正在考虑通过 .htaccess 重定向到 iPhone.domain.com.

Everything is working fine, but I now want to add a site targeted for mobile devices. While the iPhone renders the website correctly, it would be nice to provide a more targeted experience. Ideally, I'm thinking about having an iPhone.domain.com which would be redirected to via .htaccess.

为此,我正在考虑为每个设备添加另一个视图
/iPhone
   home.rhtml
   about.rhtml
   display.rhtml

For this, I was thinking about adding another view for each device
/iPhone
   home.rhtml
   about.rhtml
   display.rhtml

然而,感觉很多数据会重复,例如关于页面会在两个地方.我想我可以有一个部分并做一些类似 render :partial => 'home/about' 的事情,但这似乎有点 hacky.

However, it feels like a lot of the data would be duplicated, for example the about page would be in two places. I guess I could have a partial and do something like render :partial => 'home/about' but that seems a little hacky.

我如何开发我的网站来支持这一点?

How can I develop my site to support this?

我正在考虑一个结构,例如,但又不确定如何构建代码 - 我如何告诉它在 iPhone 目录中呈现视图......同时没有应用主布局
/显示
   /iphone
     index.rhtml

I was thinking about a structure such as, but again not sure how to structure the code - how do I tell it to render the view in the iPhone directory... while not having the master layout applied
/display
   /iphone
      index.rhtml

我真的很想就解决这个问题和构建应用程序的最佳方式提供一些建议.虽然应用程序目前遵循一个结构,但它们可能会朝着不同的方向发展.

I would really like some advice on the best way to approach this and structure the application. While the applications follow a structure at the moment, they could go off in different directions..

谢谢

推荐答案

我强烈建议让所有设备类型的控制器结构保持相同.特别是如果您使用 Rails 的 RESTful 路由,您的控制器应该与数据的域模型紧密匹配.该数据是否随后呈现给桌面浏览器、iPhone、不同类型的移动设备、JSON/XML REST API 客户端等主要是呈现层的问题,而不是控制器/路由层.

I would strongly recommend leaving the controller structure the same across all device types. Particularly if you are using Rails' RESTful routes your controllers should be closely matched to the domain model of your data. Whether that data is then presented to a desktop browser, to an iPhone, to a different type of mobile device, to a JSON/XML REST API client etc. is mostly a matter of the presentation layer, not the controller/routing layer.

所以一个优雅的解决方案是:

So an elegant solution would be:

  1. 基于用户代理检测设备类型(您可能需要参考 WURFL 用户代理数据库);
  2. 使用 Rails 的 respond_to 机制为每种设备类型呈现不同的视图格式;
  3. 为每种设备类型定义布局(例如,为移动设备使用 XHTML 移动配置文件文档类型);
  4. 根据设备类型包含不同的 CSS 文件.
  1. Detect device type based on User Agent (you may want to refer to the WURFL User Agent database);
  2. use Rails' respond_to mechanism to render a different view format for each device type;
  3. define a layout for each device type (e.g. using the XHTML Mobile Profile doctype for mobile devices);
  4. include different CSS files depending on device type.

有一些插件试图让这更容易:看看 brendanlim 的 Mobile Fu 和 noelrappin 的 Rails iUI(都在 GitHub 上).此外,Brendan Lim 在 Rails Underground 的演讲 也有一些想法.

There are some plugins which try to make this easier: have a look at brendanlim's Mobile Fu and noelrappin's Rails iUI (both on GitHub). Also Brendan Lim's presentation at Rails Underground has a few ideas.

你的目标应该是:

def show
  @foo = Foo.find(params[:id])
  respond_to do |format|
    format.html       # => show.html.erb
    format.iphone     # => show.iphone.erb
    format.blackberry # => show.blackberry.erb
  end
end

如果移动设备上的用户确实想查看网站的桌面版本,您还应该允许他们覆盖用户代理检测.具有较长过期时间的 cookie 可能是执行此操作的最佳方式,以便站点在用户下次返回时记住该选择.一些移动设备支持垃圾 cookie,但他们可能无论如何都不想要网站的桌面版本,因为它可能无法工作.

You should also allow users on mobile devices to override the user agent detection if they really want to see the desktop version of the site. A cookie with a long expiry time is probably the best way to do this, so that the site remembers the choice next time the user returns. Some mobile devices have rubbish cookie support, but then they probably won't want the desktop version of the site anyway because it probably won't work.

这篇关于Ruby on Rails 视图的移动版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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