Sometimes it happens that our application doesn't support a browser with an older version, in that case, we wish to restrict a user from running our application on an older version. In rails, we have a solution to for this problem.
Browsernizer is the gem that restricts an application to be open on old versions of the browser, and if a user tries to open the application on an older version, it will send a notification to the user to upgrade browser's version.
Lets install it, by adding following line to your Gemfile and run bundle install:
gem 'browsernizer'
Once the gem is installed, link it with:
rails generate browsernizer:install
After this open a file config/initializers/browsernizer.rb created by browsernizer.It will look something like this:
Rails.application.config.middleware.use Browsernizer::Router do |config|
config.supported "Internet Explorer", "8"
config.supported "Firefox", "4"
config.supported "Opera", "11.1"
config.supported "Chrome", "7"
config.location "/upgrade_browser.html"
config.exclude %r{^/assets}
end
This Initializer file is specifying that this our application will support IE8+, FF4+, Opera 11.1+ and Chrome 7+. The browsers that are not supported by the application can be redirected to any page that you specify in config.location option in initializer file. In our case it will be redirected to the upgrade_browser.html page, that could be a dynamic or static file.
In some cases we need to completely block some browsers from accessing our application. In such cases we need to set browser to false.
Let say we need to block Firefox browser:
config.supported "Firefox", false
Another way of restricting browsers is by accessing browsernizer information from request.env['browsernizer'] in the controller.
For example set a before filter to display notification:
before_filter :upgrade_browser
def upgrade_browser
unless request.env['browsernizer']['supported']
flash.notice = "Please upgrade your browser"
end
end
0 Comment(s)