Module: BackToRefererAfterForm

Defined in:
lib/back_to_referer_after_form.rb

Overview

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

example:

include "BackToRefererAfterForm"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
# 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

Instance Method Details

#destroy_stored_refererObject

Destroy the stored http referer



21
22
23
# 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 = {}) ⇒ Object

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.



36
37
38
39
# 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_refererObject

Stores the http referer into a session variable



16
17
18
# File 'lib/back_to_referer_after_form.rb', line 16

def store_referer
  session[:cud_referer] = request.referer
end