Skip to content

Latest commit

 

History

History
50 lines (34 loc) · 1.79 KB

README.rdoc

File metadata and controls

50 lines (34 loc) · 1.79 KB

Divert

This project rocks and uses MIT-LICENSE.

Currently, in order to use:

gem 'divert'

Install the migrations with

rake divert:install:migrations
rake db:migrate

Then add:

divert_with :controller_name

to the END of your routes file. The route added catches anything so needs to go last.

Finally add:

class ApplicationController < ActionController::Base
  include Divert

  rescue_from ActiveRecord::RecordNotFound do |exception|
    divert(request.env['PATH_INFO'])
  end
end

to your ApplicationController. The ‘rescue_from ActiveRecord::RecordNotFound` is optional, but allows Divert to recover from `Model.find(…)` errors with a redirect or 404.

It will not interfere with actions defined in the configured controller or view files that don’t have an action defined. Cos it’s nice like that.

By default, Divert will hit the database to find or create a record. To turn off this behaviour, add this following configuration code in your ‘config/initializers/divert.rb` :

Divert.configure do |config|
  config.save_to_db = false
end

Diverting Clientside

By default, the Divert gem will redirect from the server. However, there are times you might want to redirect from clientside, for example, if you have to do some custom analytics tracking using a clientside script, such as Google Analytics.

To turn on clientside redirects, add this following configuration code in your ‘config/initializers/divert.rb` :

Divert.configure do |config|
  config.redirect_clientside = true
end

Divert expects a view named [controller_name]/divert_clientside.html.erb, which you will need to create.

You will need to handle the clientside logic yourself. You have access to the following parameters: params[:divert_redirect_to] and params[:divert_redirect_from].