由于发生多次插入,正在发送多封电子邮件。

时间:2016-10-13 07:59:17

标签: ruby-on-rails

我有一个联系表单,用户提交他的信息,我应该发送给网站的管理员。但是有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次

  • 第二个是即使触发器是2次,验证也不应允许Contact模型再次保存相同的数据。

2 个答案:

答案 0 :(得分:1)

是create.js渲染2次?好像有2个线程正在运行..

答案 1 :(得分:0)

试试这个,它会对你有用

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