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

查看:127
本文介绍了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

/ display

    index.rhtml

/ data < - 这是由jQuery从display \ index页面调用来提供要呈现的数据

    push.js.erb

  ;   pull.js.erb

/ layout

    home.rhtml

     ; display.rhtml

/home
   about.rhtml
   index.rhtml
/display
   index.rhtml
/data <--This is called by jQuery from the display\index 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

然而,感觉很多数据会被复制,例如关于页面将在两个地方。我想我可以有一个局部并做一些像渲染: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目录中呈现视图......虽然没有应用主布局但是
/ display

    / 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..

谢谢

Ben

推荐答案

我强烈建议在所有设备类型中保持控制器结构相同。特别是如果您使用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 Mobile Profile doctype);

  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天全站免登陆