Redirect on Back
Prevent re-submission of a form by redirecting after user hits the back button. For Rails 3.1 and above.
Example Scenario - User Registration
- User registers by filling in form and hits 'submit'.
- User wants to change one of his details.
- User hits 'back'.
- User changes something and hits 'submit' again.
- Arrrggg... a new User record has been created instead of updating the existing one.
Installation (Rails 3.1 and higher)
Add this line to your application's Gemfile:
gem 'redirect_on_back'
Then execute:
$ bundle
And then specify the use of the redirect_on_back
javascripts in your application.js
:
//= require redirect_on_back
Usage
In your controller actions:
def create
@user = User.new(params[:user])
if result = @user.save
redirect_on_back_to edit_user_path(@user) # If user hits 'back' he'll be redirected to edit_user_path
redirect_to @user
else
render :action => :new
end
end
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request