/redirectr

Provides Rails-helper methods for referrer-style backlinks and setting redirect URLs after form submussion

Primary LanguageJavaScriptMIT LicenseMIT

= Redirectr

In many web applications, the user triggers actions that result in simple or complex workflows that should, after that workflow is finished, result in the user being redirected to the page where he initially started it. Another example would be a "back"-Link on any page.
A simple but completely Un-RESTful way would be to store the "current" page in a cookie each time the user calls an action and redirect to the url stored there if needed.

A much better (and potentially even cacheable) way is to encode the "backlink" URL in an URL parameter or form field and pass it along with every workflow step until the last (or only) action uses it to redirect the user back to where he initially came from.

Redirectr really does nothing more than provide a simple API for exactly that.

Redirectr provides a few Controller and Helper methods that will be included in your ApplicationController and ApplicationHelper, respectively.

== Installation

With Rails 3, just at the following to your Gemfile

    gem 'redirectr', :git => 'https://github.com/wvk/redirectr.git'

and then call

    bundle install

== Example

Suppose you have an application with a contact form that can be reached via a footer link on every page. After submitting the form, the user should be redirected to the page he was before clicking on the "contact form" link.

for the footer link to the contact form:

    <%= link_to 'Contact us!', new_contact_path(referrer_param => current_path) %>

In the 'new contact' view:

    <%= form for ... do |f| %>
      <%= hidden_referrer_input_tag %>
      <!-- ... -->
    <% end %>

and finally, in the 'create' action of your ContactsController:

    def create
      # ...
      redirect_to back_or_default
    end

for more detailled examples, see the Rdoc documentation.


== Changelog

0.0.7: Add Rails 2 compatibility (Thanks to Falk Hoppe)

Copyright (c) 2010 Willem van Kerkhof <wvk@consolving.de>, released under the MIT license