Skip to content
Prevent from submitting form twice for Rails
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
spec
.gitignore
.rspec
.travis.yml
CODE_OF_CONDUCT.md
Gemfile
LICENSE.txt
README.md
Rakefile
submit_once.gemspec

README.md

SubmitOnce Build Status

Prevent from submitting form twice for Rails.

Scenario: when user wanna add a post, usually go to new page first, input some information and submit, it will redirect to success page if these is no error. However after all if user click backward button on browser, it will render history page(the new page before), user could submit same form again.

SubmitOnce gem resolve this problem. Form could be submitted only browser fetch a new fresh one.

Installation

Add this line to your application's Gemfile:

gem 'submit_once'

Usage

posts/new.html.erb

<%= form_for @post do |f| %>
  <!-- token tag here -->
  <%= form_token_tag %>

  <%= f.text_field :title %>
  ...
  <%= f.submit %>
<% end %>

posts_controller.rb

class PostsController < ApplicationController
  # return to root_url when form submit again
  before_action :check_form_token!, only: [:create]

  # OR custom before action
  # before_action :custom_check_form_token

  def index
  end

  def new
  end

  def create
    ...
  end

  private
  def custom_check_form_token
    # using `check_form_token`
    unless check_form_token
      redirect_to posts_url, alert: 'Invalid form token'
    end
  end
end

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/submit_once. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

You can’t perform that action at this time.