gates
gates

Reputation: 4603

Multiple emails are being sent due to Multiple insertions happening.

I have a contact form, in which user submits his information, I am supposed to send to admins of the site. But 2 emails are getting sent to the admins, instead of one

```

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"rJr/Z9zM21HhKhiQ0vw2afHKkTchUgDAxnvm13s3pD3jb8Elu/B65pHN2BUTUCwLauowgOum4JdyGeNdCsi3QQ==", "contact"=>{"name"=>"vamsi gates", "mobile"=>"07639817688", "email"=>"[email protected]", "requirement"=>"hello, my name is bamsi"}, "commit"=>"submit"}
   (0.2ms)  BEGIN
  Contact Exists (0.6ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."name" = $1 AND "contacts"."mobile" = $2 AND "contacts"."requirement" = $3 AND "contacts"."email" = $4 LIMIT $5  [["name", "vamsi gates"], ["mobile", "07639817688"], ["requirement", "hello, my name is bamsi"], ["email", "[email protected]"], ["LIMIT", 1]]
Started POST "/contacts" for 127.0.0.1 at 2016-10-13 13:20:03 +0530
Processing by ContactsController#create as JS
  SQL (2.2ms)  INSERT INTO "contacts" ("name", "mobile", "email", "requirement", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "vamsi gates"], ["mobile", "07639817688"], ["email", "[email protected]"], ["requirement", "hello, my name is bamsi"], ["created_at", 2016-10-13 07:50:03 UTC], ["updated_at", 2016-10-13 07:50:03 UTC]]
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"rJr/Z9zM21HhKhiQ0vw2afHKkTchUgDAxnvm13s3pD3jb8Elu/B65pHN2BUTUCwLauowgOum4JdyGeNdCsi3QQ==", "contact"=>{"name"=>"vamsi gates", "mobile"=>"07639817688", "email"=>"[email protected]", "requirement"=>"hello, my name is bamsi"}, "commit"=>"submit"}
   (0.2ms)  BEGIN
  Contact Exists (0.5ms)  SELECT  1 AS one FROM "contacts" WHERE "contacts"."name" = $1 AND "contacts"."mobile" = $2 AND "contacts"."requirement" = $3 AND "contacts"."email" = $4 LIMIT $5  [["name", "vamsi gates"], ["mobile", "07639817688"], ["requirement", "hello, my name is bamsi"], ["email", "[email protected]"], ["LIMIT", 1]]
  SQL (0.5ms)  INSERT INTO "contacts" ("name", "mobile", "email", "requirement", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["name", "vamsi gates"], ["mobile", "07639817688"], ["email", "[email protected]"], ["requirement", "hello, my name is bamsi"], ["created_at", 2016-10-13 07:50:03 UTC], ["updated_at", 2016-10-13 07:50:03 UTC]]
   (51.0ms)  COMMIT
  Rendering guest_mailer/query_email.html.erb within layouts/mailer
  Rendered guest_mailer/query_email.html.erb within layouts/mailer (0.5ms)
GuestMailer#query_email: processed outbound mail in 8.0ms
   (81.0ms)  COMMIT
  Rendering guest_mailer/query_email.html.erb within layouts/mailer
  Rendered guest_mailer/query_email.html.erb within layouts/mailer (0.1ms)
GuestMailer#query_email: processed outbound mail in 7.1ms
Sent mail to [email protected], [email protected], [email protected] (20368.0ms)
Date: Thu, 13 Oct 2016 13:20:03 +0530
From: [email protected]
To: [email protected], 
 [email protected], 
 [email protected]
Message-ID: <[email protected]>
Subject: We have a new lead from vamsi gates
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
      <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700' rel='stylesheet' type='text/css'>
      <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700' rel='stylesheet' type='text/css'>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=device-dpi, user-scalable=0">
      <title>New enquiry for digifutura</title>
  </head>
  <body>
    Name: vamsi gates <br>
    Mobile: 07639817688 <br>
    Email: [email protected] <br>
    Requirement brief: <br>
    hello, my name is bamsi
  </body>  
  </html>


  </body>
</html>

  Rendering contacts/create.js.erb
Sent mail to [email protected], [email protected], [email protected] (20346.5ms)
Date: Thu, 13 Oct 2016 13:20:03 +0530
From: [email protected]
To: [email protected], 
 [email protected], 
 [email protected]
Message-ID: <[email protected]>
Subject: We have a new lead from vamsi gates
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <style>
      /* Email styles need to be inline */
    </style>
  </head>

  <body>
    <!DOCTYPE html>
<html lang="en">
  <head>
      <meta charset="utf-8">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
      <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700' rel='stylesheet' type='text/css'>
      <link href='https://fonts.googleapis.com/css?family=Roboto+Slab:400,300,700' rel='stylesheet' type='text/css'>
      <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, target-densitydpi=device-dpi, user-scalable=0">
      <title>New enquiry for digifutura</title>
  </head>
  <body>
    Name: vamsi gates <br>
    Mobile: 07639817688 <br>
    Email: [email protected] <br>
    Requirement brief: <br>
    hello, my name is bamsi
  </body>  
  </html>


  </body>
</html>

```

But I have validations in place for the contact model, which should prevent these kinds of insertions. In the console I am not unable to create a new record with the same information, but the records are getting inserted in the rails server

class Contact < ApplicationRecord
    validates :name, presence: true 
    validates :mobile, presence: true
    validates :requirement, presence: true
    validates :email, presence: true
    validates_uniqueness_of :name, scope: [:mobile, :requirement, :email]
end

Here is the contacts controller

class ContactsController < ApplicationController
  def create
    @contact = Contact.new(get_params)
    if @contact.save! 
        GuestMailer.query_email(params[:contact][:name],
            params[:contact][:mobile],
            params[:contact][:email],
            params[:contact][:requirement]).deliver
    end
  end

  private 

  def get_params
    params.require(:contact).permit(:name, :mobile, :email, :requirement)
  end
end

Here is the form

<div class="col-xs-12 col-sm-4 col-sm-offset-4">
    <%= simple_form_for :contact,url: contacts_path,html: {id: 'contact-form'}, authenticity_token: true, remote: true do |f| %>        
        <%= f.input :name, placeholder: 'Your Name', requried: true, label: false %>
        <%= f.input :mobile, placeholder: 'Your Mobile Number (optional)', label: false, required: false %>
        <%= f.input :email, placeholder: 'Your email id', required: true, label: false %>
        <%= f.input :requirement, placeholder: 'Your Requirement in Brief', required: true, label: false, as: :text %>
        <%= f.submit :submit, class: 'btn cta' %>
    <% end %>
</div>

Here is the create.js.erb

$('.contact-form').hide();
$('.confirmation-message').show();
$(".cta").attr("disabled", true);

Problems:

Upvotes: 0

Views: 78

Answers (2)

Ankita Agrawal
Ankita Agrawal

Reputation: 422

is create.js rendering 2 times? It seems like 2 threads are running ..

Upvotes: 1

Suneel
Suneel

Reputation: 381

Try this, It will work for you

<%= f.submit :submit, class: 'btn cta', data: { disable_with: "Processing" } %>

Upvotes: 0

Related Questions