module BackToRefererAfterForm

This module should be included in ApplicationController (or in any other)

example:

include "BackToRefererAfterForm"

Public Class Methods

included(base) click to toggle source
# File lib/back_to_referer_after_form.rb, line 7
def self.included(base)
  # Stores the referer after the new, edit or destroy
  base.before_filter :store_referer, :only => [:new, :edit, :destroy]

  # Destroy the stored referer after redirecting or if any other action is called in the middle
  base.before_filter :destroy_stored_referer, :only => [:index]
end

Public Instance Methods

destroy_stored_referer() click to toggle source

Destroy the stored http referer

# File lib/back_to_referer_after_form.rb, line 21
def destroy_stored_referer
  session[:cud_referer]=nil
end
redirect_back_or_to(options={},response_status={}) click to toggle source

Redirects the browser to the stored referer. If there’s no stored referer will fallback to the the target specified in options.

Redirecting :back using redirect_to will redirect to the <strong>action</strong> referer, which is the form.

Redirecting with redirect_back_or_to :target will redirect to the <strong>form</strong> referer if possible. this is userful when calling a form from other controller and want to get back to where you came from after the update/create/destroy action.

# File lib/back_to_referer_after_form.rb, line 36
def redirect_back_or_to(options={},response_status={})
  options=session[:cud_referer] if session && session[:cud_referer]
  redirect_to(options,response_status)
end
store_referer() click to toggle source

Stores the http referer into a session variable

# File lib/back_to_referer_after_form.rb, line 16
def store_referer
  session[:cud_referer] = request.referer
end