Mobile version of views for Ruby on Rails

前端 未结 4 1658
长情又很酷
长情又很酷 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:01

    Rails 4.1 includes Variants, a great new feature that:

    Allows you to have different templates and action responses for the same mime type (say, HTML). This is a magic bullet for any Rails app that's serving mobile clients. You can now have individual templates for the desktop, tablet, and phone views while sharing all the same controller logic.

    In your case, you only need to set the variant for the iphone in a before_action, e.g:

    class HomeController < ApplicationController
      before_action :detect_iphone
      def index
    
        respond_to do |format|
          format.html               # /app/views/home/index.html.erb
          format.html.phone         # /app/views/home/index.html+phone.erb
        end
      end
    
      private
        def detect_iphone
          request.variant = :iphone if request.user_agent =~ /iPhone/
        end
    end
    

    What's new in Rails 4.1

提交回复
热议问题