render different view on mobile/tablet
Rails
https://gorails.com/episodes/rails-request-variants?autoplay=1
https://github.com/gorails-screencasts/request-variants/commit/78d72b59a0a35ce4df2de8dcb0626001bfc87a5e
https://github.com/gorails-screencasts/request-variants/blob/master/app/controllers/concerns/detect_device.rb
# a/c/concerns/detect_device.rb
module DetectDevice
extend ActiveSupport::Concern
included do
before_action :set_variant
end
def set_variant
case request.user_agent
when /iPhone/
request.variant = :phone
when /iPad/
request.variant = :tablet
end
end
end..or ther is browser gem that better handles different devices
browser = Browser.new(request.user_agent)
if browser.mobile?
request.variant = :phone
elsif browser.tablet?
request.variant = :tablet
endclass ApplicationController < ActionController::Base include DetectDevice #...
views
then you need two partials
when variant is set RAILS renders different partial
controller side
if @post.save
format.html.phone { redirect_to @post, notice: "Post was successfully created ON PHONE." }
format.html { redirect_to @post, notice: "Post was successfully created." }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
end