Несколько писем отправляются из-за множественных вставок.

Несколько писем отправляются из-за множественных вставок.

У меня есть контактная форма, в которой пользователь отправляет свою информацию, я должен отправить администратору сайта. Но 2 письма отправляются администраторам, а не одному

`` `

 Parameters: {"utf8"=>"?", "authenticity_token"=>"rJr/Z9zM21HhKhiQ0vw2afHKkTchUgDAxnvm13s3pD3jb8Elu/B65pHN2BUTUCwLauowgOum4JdyGeNdCsi3QQ==", "contact"=>{"name"=>"vamsi gates", "mobile"=>"07639817688", "email"=>"gvpmahesh@outlook.com", "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", "gvpmahesh@outlook.com"], ["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", "gvpmahesh@outlook.com"], ["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"=>"gvpmahesh@outlook.com", "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", "gvpmahesh@outlook.com"], ["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", "gvpmahesh@outlook.com"], ["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 gvpmahesh@gmail.com, mahesh@digifutura.com, gvpm1994@gmail.com (20368.0ms)
Date: Thu, 13 Oct 2016 13:20:03 +0530
From: tech@digifutura.com
To: gvpmahesh@gmail.com, 
 mahesh@digifutura.com, 
 gvpm1994@gmail.com
Message-ID: <57ff3cabc6732_5c9b1a24e40470c@gates.mail>
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: gvpmahesh@outlook.com <br>
    Requirement brief: <br>
    hello, my name is bamsi
  </body>  
  </html>


  </body>
</html>

  Rendering contacts/create.js.erb
Sent mail to gvpmahesh@gmail.com, mahesh@digifutura.com, gvpm1994@gmail.com (20346.5ms)
Date: Thu, 13 Oct 2016 13:20:03 +0530
From: tech@digifutura.com
To: gvpmahesh@gmail.com, 
 mahesh@digifutura.com, 
 gvpm1994@gmail.com
Message-ID: <57ff3cabcef05_5c9b1a24b9847124@gates.mail>
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: gvpmahesh@outlook.com <br>
    Requirement brief: <br>
    hello, my name is bamsi
  </body>  
  </html>


  </body>
</html>

`` `

Но у меня есть валидации для контактной модели , которая должна предотвращать подобные вставки. В консоли я не могу создать новую запись с той же информацией, но записи вставляются в сервер rails

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

Вот контроллер контактов

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

Вот форма

<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>

Здесь create.js.erb

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

Проблемы:

  • Я не уверен, почему происходит 2 раза вставка, что заставляет его запускать 2 раза

  • И во-вторых, хотя триггер имеет два раза, проверки не должны позволять модели Contact сохранять одни и те же данные снова.

ruby-on-rails,

0