Mobile version of views for Ruby on Rails

前端 未结 4 1666
长情又很酷
长情又很酷 2020-12-22 20:04

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<

4条回答
  •  佛祖请我去吃肉
    2020-12-22 21:07

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

    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.

    What you should be aiming for is something like:

    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
    

    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.

提交回复
热议问题