Trying to make a mobile version of the site with the subdomain "m".

ApplicationController:

class ApplicationController < ActionController::Base before_filter :redirect_to_mobile def redirect_to_mobile if mobile_browser? && !mobile_request? redirect_to "m." + request.host_with_port.gsub(/^www\./, '') and return end end def mobile_browser? request.env["HTTP_USER_AGENT"] && request.env["HTTP_USER_AGENT"][/(iPhone|iPod|iPad|Android)/] end helper_method :mobile_browser? def mobile_request? request.subdomain.split('.')[0] == 'm' end helper_method :mobile_request? end 

Redirect goes to the right address, but the page does not load. I use the android emulator from sdk, I get "This site can`t be reached".

 Started GET "/" for 192.168.1.47 at 2017-01-24 20:24:47 +0300 Cannot render console from 192.168.1.47! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by WelcomeController#index as HTML Parameters: {"subdomain"=>""} request.url: http://test.ru:3000/ ITS Mozilla/5.0 (Linux; Android 7.1; Android SDK built for x86_64 Build/NPF26K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.135 Mobile Safari/537.36 Redirected to m.test.ru:3000 Filter chain halted as :redirect_to_mobile rendered or redirected Completed 302 Found in 1ms (ActiveRecord: 0.0ms) 

If the same address is opened manually, everything is loaded correctly.

  • one
    and if you open in the browser and change the user agent to the one you need, will there be a redirect? - Mikhail Vaysman
  • @MikhailVaysman The page is also not loaded, but there is a difference, in the emulator the url in the browser changes from test.ru to m.test.ru, nothing happens in the desktop browser with the user agent android. - Tiazar
  • one
    try adding //. like this "// m." - Mikhail Vaysman
  • @MikhailVaysman Your comment helped me see what I missed. Added to request.protocol redirect, now works. - Tiazar
  • If possible, publish the solution found in response to your question . I am sure it will help many of your colleagues in the future. - Mikhail Vaysman

1 answer 1

In the end, kosyachny method, in the corrected (working) form now looks like this

 def redirect_to_mobile if mobile_browser? && !mobile_request? redirect_to request.protocol + "m." + request.host_with_port + request.path and return end end 
  • Unclear, is this the answer or the newer version of the non-working code? The answer field is for answers only. - AK
  • @AK is a fixed code that does what I need. - Tiazar