Ruby on Rails Mobile App

I am trying to develop a Ruby on Rails application that detects a client, that is, a mobile (browser) that connects to the server and displays the appropriate layout. I tried using the following link but could not connect it. Any suggestions?

http://www.arctickiwi.com/blog/mobile-enable-your-ruby-on-rails-site-for-small-screens

I am using Opera Mini emulator to test the application.

+3
source share
1 answer

The most elegant solution for this that I have seen is to do two things: recognize mobile users based on the user agent in the request and use the custom mime Rails type for response, allowing you to customize HTML templates for mobile users.

Define a custom โ€œmobileโ€ Rails MIME type in config/initializers/mime_types.rb , which is just HTML:

 Mime::Type.register_alias "text/html", :mobile 

Add a helper / filter to answer mobile users:

 def mobile_user_agent? @mobile_user_agent ||= ( request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(Mobile\/.+Safari)/] ) end 

then ..

 before_filter :handle_mobile def handle_mobile request.format = :mobile if mobile_user_agent? end 

Create your own mobile template:

 app/views/users/show.html.erb => app/views/users/show.mobile.erb 

Sending mobile or regular in controllers:

 respond_to do |format| format.html { } # Regular stuff format.mobile { } # other stuff end 
+7
source

Source: https://habr.com/ru/post/1389915/


All Articles