1

I have a controller action with the following code:

class ContentsController < ApplicationController
  def index
  end

  def submit
  end

  def input_data
    if browser.device.mobile?
      @mobile = true
    else
      @mobile = false
    end
    @keyword = params[:keyword]

    @ip = request.ip
    Visitor.create(:template => @template,...., :screenh => @screenh)
  end

  def update
    @visitor = Visitor.order("created_at").last
    if @visitor.update(visitor_params)
      InquiryMailer.inquiry(@visitor).deliver_now
      render json: @visitor
    else
      flash[:alert] = "Unsuccessful - Contact Us By Phone."
    end         
  end

  private
    def visitor_params
      params.require(:visitor).permit(:name, :email, :message, :phone)
    end
end

And an mailer class that looks like this..

class InquiryMailer < ApplicationMailer
  default from: "admin@example.com"

  def inquiry(user)
    @user = user
    @user.keyword = 'New Inquiry' unless @user.keyword
    mail(from: 'admin@example.com', 
         subject: @user.keyword.titleize, 
         to: 'richard@example.com', 
         reply_to: @user.email)
  end   
end

I am trying to determine why my Rails 5 app is sending the inquiry email twice. To give some background, the input_data action runs after the page is loaded and the update action runs when a form is submitted. The logs clearly show two E-mails but I cannot figure out why.

Thank you.

rogelio
  • 1,541
  • 15
  • 19
Joe D
  • 297
  • 1
  • 12
  • 1
    check if the form was sent only once (open the developer tools -> network in your browser) – rogelio Mar 08 '17 at 22:05
  • I cannot be certain but do not think the JS is submitting the form 2x. I added this code to make sure http://stackoverflow.com/questions/2830542/prevent-double-submission-of-forms-in-jquery. – Joe D Mar 08 '17 at 22:11
  • Actually - It has running the code 2 times. A post and patch... – Joe D Mar 08 '17 at 22:15
  • Thank you. I saw the mistake! – Joe D Mar 08 '17 at 22:19
  • good! can you update your questions for future references? Maybe someone some day will have the same/similar problem – rogelio Mar 09 '17 at 02:49

0 Answers0