Reputation: 1027
I have follow this tutorial to do:
http://railscasts.com/episodes/199-mobile-devices?view=asciicast
Now I have app/views/layouts/pc.html.erb
and app/views/layouts/mobile.html.erb
.
I tried to add these code to application_controller.rb:
private
def mobile_device?
if session[:mobile_param]
session[:mobile_param] == "1"
else
request.user_agent =~ /Mobile/
end
end
def prepare_for_mobile
session[:mobile_param] = params[:mobile] if params[:mobile]
request.format = :mobile if mobile_device?
end
protected
def layout_by_device
if mobile_device?
render layout: "mobile"
else
render layout: "pc"
end
end
And in my products_controller.rb:
before_action :layout_by_device
def show
respond_to do |format|
format.html
format.mobile
end
end
But I can only see my mobile layout's content, I can't see product page's content with :yield
.
If I remove before_action :layout_by_device
and use render layout: 'mobile'
in my products_controller.rb, I can see the product page's content but can't see the mobile layout's.
Maybe this is about Rails' respond_to
and render
. How to do?
Upvotes: 1
Views: 2631
Reputation: 1027
I found the right solution:
2.3 Action Pack Variants is what I wanted.
request.variant
Upvotes: 1
Reputation: 1050
Not trying to discourage you from following the tutorial, but if your goal is to adjust your app to small screens and their limitations, the approach of maintaining separate layouts/views is rather outdated. Your time would be better spent on learning responsive webdesign, which is the concept of letting page elements grow/shrink with the available screen size.
Upvotes: 2