Reputation: 911
In our application we have several pages that are available in mobile format but not all of them are.
So I decided to follow that tutorial : A Better Way to Add Mobile Pages which works well.
When a user is using a mobile device, mobile views are rendered if they are available, if not they will be shown the desktop version.
However I have a issue with the layout. My desktop layout and my mobile layout are different so when a mobile page is rendered I would like to use my mobile layout and when the desktop version is displayed I would like to use my desktop layout even when the user is on a mobile device.
I added the following code to my application controller (I also added the code in this link A Better Way to Add Mobile Pages ) to detect if the user is mobile or not and to change the layout accordingly :
layout :determine_layout
def mobile_device?
if session[:mobile_override]
session[:mobile_override] == "1"
else
# Season this regexp to taste. I prefer to treat iPad as non-mobile.
(request.user_agent =~ /(iPhone|iPod|Android|webOS|Mobile)/) && (request.user_agent !~ /iPad/)
end
end
helper_method :mobile_device?
def determine_layout
if mobile_device?
"mobile"
else
"application"
end
end
However the issue with this code is that even if the page is not available in view_mobiles, it will try to display the mobile layout, as I am checking for a mobile device and not a mobile page.
How can I determine layout based on the view rendered ? or if the view exist ?
My mobile views are located in the view_mobiles folder. I would like to check if the mobile view is rendered instead of the the mobile device and display the corresponding layout.
Is that possible ?
Thanks a lot,
Upvotes: 5
Views: 4908
Reputation: 911
Ok so here is what I finally found and how I got it working with devise :
I can now create mobile views in the same folder as the regular views, and if the mobile view does not exist it will fallback to the regular view.
Here is the two links you need to get everything working :
Mobile Format Fallback to default views if mobile view is missing
Upvotes: 2