Release of Rails 4.1.0.beta1 brings in a few features namely Variants, Spring, mailer previews, JS CSRF, config/secrets.yml, Enums. Godfrey as a good post with description of all features here.
So here I am going to try and explain use of Variants with Mobvious to decide device type and render view accordingly.
1) Let me start with mobvious :-
Mobvious detects whether your app / website is being accessed by a phone, or by a tablet, or by a personal computer.
Basic installation :-
There is a good plugin especially made for rails mobvious-rails.
So after adding mobvious we can use method request.env[‘mobvious.device_type’] to get the current type of device i.e., desktop, tablet or mobile.
2) Now we have to set the variant in application_controller.rb
This will set request.variant value to its respective device.
3) Now the easy part we have tell controller which partial to load according to request.variant value. That can be done in two ways :-
4) Just create the views according to the format name. lets say we want to create index view for tablet we can create that view as index.html+tablet.erb which will be automatically detected by rails controller by default.
Thats it. Checkout the demo app I created for the same Variant-demo and source code.
I have attached a few screenshots below :-
This is my first blog post hope you guys like it. Looking forward to your feedback.