0
votes

New to Rails here. I got a short script in Rails at detectmobilebrowsers.com to check and redirect mobile browsers. It looks like:

def redirect_mobile(url = "http://detectmobilebrowser.com/mobile") redirect_to url ..... ..... (request.user_agent[0..3]) end

It looks like this is one big function. Where should I put this in the typical Rails directory structure (app with controller, helpers, models subdirectories / components / config / etc) so that when a user accesses the front page (index), they'll get redirected to a certain mobile address?

Thanks!

1

1 Answers

0
votes

You can set this up as a before_filter on your ApplicationController. This controller is inherited by all of your other controllers, so its filters run on every request.

Under app/controllers/application_controller.rb,

class ApplicationController < ActionController::Base
  before_filter :redirect_mobile

private

  def redirect_mobile(...)
    # ...
  end
end

Though, I highly suggest that you modify that script to do smarter redirects, otherwise a mobile user visiting http://www.example.com/products/123?color=green will simply be redirected to the mobile homepage, rather than the mobile version of product 123 (in green!).