The Back Button and multiple form submits

Pretty standard stuff:

  1. User fills out form
  2. User submits form
  3. User thinks “doh! an error!”
  4. User hits the browser back button
  5. User updates the data in the form
  6. User thinks he is updating the data by hitting submit again, but
    inserts another record.

Now we have two records in the database, one is the correct one, and one
is the bad one. How do we know witch one is the good one? The second
one! I hear you all scream. But does that mean we should delete the
first one? How do you deal with this situation?
I think it is quite obvious what the user intended to do, and we should,
in our webapp, do what the user wanted to do, not annoy him with stupid
error messages like “Sorry, you can’t submit the same form twice”, or
“Sorry, a record with this ID already exists in our database”
Is there a nice solution in rails to this very frequent problem in web
applications?

Thanks

It is a difficult problem to deal with, since Tech-savvy users will
press the BACK button when they actually WANT a second record in the
database!

One method I have used is to redirect to the edit page after a create
has been performed. For example

def create
@object = Object.new
if @object.save
redirect_to :action => ‘edit’
eles
render :action => ‘new’
end
end

then they aren’t tempted to press back because they are already at the
edit page.

I hope that helps.

DyingToLearn wrote:

It is a difficult problem to deal with, since Tech-savvy users will
press the BACK button when they actually WANT a second record in the
database!

One method I have used is to redirect to the edit page after a create
has been performed. For example

def create
@object = Object.new
if @object.save
redirect_to :action => ‘edit’
eles
render :action => ‘new’
end
end

then they aren’t tempted to press back because they are already at the
edit page.

I hope that helps.

I found out that FF and IE “disable” the back button for a particular
form if you submit the form to the page that rendered the form itself (I
typed this by hand, it could not work as written here, but the concept
is valid):

$ rails test
$ cd test
$ ruby script/generate controller back
$ cat > app/controllers/back_controller.rb
class BackController < ApplicationController

def backtest
if request.method == :post
session[:state] = ‘saved’
redirect_to :action => ‘backtest’
end
end

def clear
session[:state] = nil
redirect_to :action => ‘backtest’
end
end

^D

$ cat > app/views/back/backtest.html.erb

Backtest <% if session[:state] %> <%= 'Record saved' %> <% else %> <% form_tag :action => "backtest" do %> <% end %> <% end %>

^D

Now, if you go to
http://localhost:3000/back/backtest
and hit submit, you get the ‘Record saved’ message, but the back button
of the browser does not take you back to the form.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs