0.0
No commit activity in last 3 years
No release in over 3 years
Prevent from submitting form twice for rails.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.11
>= 0
~> 10.0
~> 3.0

Runtime

< 5, >= 3.2
 Project Readme

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.