Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise registration submitting form twice?

I have a Devise registration form that is submitting twice when I press the submit button. Initially, I thought the issue was turbolinks, but I tried disabling it and the server logs still shows Users::RegistrationsController#create getting hit twice.

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-signin', role: 'form'}) do |f| %>
  <%= devise_error_messages! %>
  <% if current_or_guest_user.present? %>
    <%= f.text_field :name, :autofocus => true, :class => 'form-control', :placeholder => 'Full Name', :value => current_or_guest_user.name %>
    <%= f.email_field :email, :class => 'form-control', :placeholder => 'Email Address', :value => current_or_guest_user.unconfirmed_email %>
    <%= f.text_field :postal_code, :autofocus => true, :class => 'form-control', :placeholder => 'Zip Code', :value => current_or_guest_user.postal_code %>
  <% end %>
  <%= f.submit 'Sign Up', class: 'btn btn-lg btn-primary btn-block } %>
<% end %>

The odd thing is that if I add a puts "COW into my controller action, I only see it get outputted once in the server logs. However, there are two different time stamps that calls the controller so I'm confident it's getting called twice.

def create
  puts "COW!!!"
  if current_or_guest_user
    current_or_guest_user.update_attributes(name: params[:user][:name], email: params[:user][:email], postal_code: params[:user][:postal_code]) if (current_or_guest_user.pseudo? || current_or_guest_user.guest?)
    redirect_to service_region_path(current_or_guest_user.current_service_region.url_name), notice: "Please check your email for a confirmation link to complete your registration" and return
  else
    super
  end
end

Server output

I, [2014-06-11T19:38:19.043603 #8159]  INFO -- : Started POST "/users" for 127.0.0.1 at 2014-06-11 19:38:19 -0700
I, [2014-06-11T19:38:19.043629 #8159]  INFO -- : Started POST "/users" for 127.0.0.1 at 2014-06-11 19:38:19 -0700
I, [2014-06-11T19:38:19.049479 #8159]  INFO -- : Processing by Users::RegistrationsController#create as HTML
I, [2014-06-11T19:38:19.049536 #8159]  INFO -- : Processing by Users::RegistrationsController#create as HTML
I, [2014-06-11T19:38:19.049571 #8159]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"4a/sJWkOtfQxaPmpnvcAYU/77mRwIDaC4bEFMvlfyHQ=", "user"=>{"name"=>"mh", "email"=>"[email protected]", "postal_code"=>"94103"}, "commit"=>"Sign Up"}
I, [2014-06-11T19:38:19.049586 #8159]  INFO -- :   Parameters: {"utf8"=>"✓", "authenticity_token"=>"4a/sJWkOtfQxaPmpnvcAYU/77mRwIDaC4bEFMvlfyHQ=", "user"=>{"name"=>"mh", "email"=>"[email protected]", "postal_code"=>"94103"}, "commit"=>"Sign Up"}
COW!!!
D, [2014-06-11T19:38:19.057844 #8159] DEBUG -- :   User Load (6.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 37569]]
D, [2014-06-11T19:38:19.057951 #8159] DEBUG -- :   User Load (6.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."id" = $1 LIMIT 1  [["id", 37569]]

My guess is that there might be some javascript that is submitting the form when I press submit, but I haven't written any custom javascript to target my forms.

application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require twitter/bootstrap
//= require bootstrap-select
//= require moment
//= require bootstrap-datetimepicker
//= require jquery.payment
//= require zeroclipboard
//= require_tree .

Do you guys have any tips on how I can dig into this issue further - how do I figure out what could possibly be making the 2nd call? I want to find the root cause of the problem and not have to patch it using some javascript to disable the form after first submission.

like image 365
Huy Avatar asked Jun 11 '26 08:06

Huy


1 Answers

Strange issue - the best thing I can suggest is it might be a turbolinks issue

The way to test this will either be to remove Turbolinks from your app for the time being, or to disable turbolinks explicitly on your form. If this works, it will generally mean you have a setting / argument somewhere which will be triggering the turbolinks activity:

#application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks <- remove
//= require twitter/bootstrap
//= require bootstrap-select
//= require moment
//= require bootstrap-datetimepicker
//= require jquery.payment
//= require zeroclipboard
//= require_tree .

#app/views/layouts/application.html.erb
stylesheet_link_tag    "application", media: "all"

#app/views/devise/registrations/new.html.erb
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), html: {class: 'form-signin', role: 'form'}, data: { no_turbolink: true }) do |f| %>
like image 182
Richard Peck Avatar answered Jun 13 '26 00:06

Richard Peck