我正在尝试利用 Rails Variants 为手机使用不同的布局,为平板电脑+台式机使用不同的布局(默认布局)。
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :detect_device_format
private
def detect_device_format
case request.user_agent
when /iPad/i
request.variant = :tablet
when /iPhone/i
request.variant = :phone
when /Android/i && /mobile/i
request.variant = :phone
when /Android/i
request.variant = :tablet
when /Windows Phone/i
request.variant = :phone
end
end
end
class HomeController < ApplicationController
def index
respond_to do |format|
format.json
format.html # /app/views/home/index.html.erb
format.html.phone # /app/views/home/index.html+phone.erb
format.html.tablet # /app/views/home/index.html+tablet.erb
end
end
end
现在,我知道我可以使用类似的东西,format.html.phone { render layout: 'application-mobile' }
但我不想每次都这样做。
我想保持干燥并创建默认的电话布局。
如何使用 Rails 4.1 完成此任务?