Question

Let us take a few line of code from controller as:

class VendorsController < ApplicationController
  def new
    @vendor = Vendor.new
  end

  def create
    @vendor = Vendor.new(params[:vendor])
    if @vendor.save
      VendorMailer.registration_confirmation(@vendor).deliver
      flash[:success] = "Vendor Added Successfully"
      redirect_to amain_path
    else
      render 'new'
    end
  end
end

Now, in the localhost when my connection in off it show me error of socket error (as expected) but if i make a condition :

 def create
   @vendor = Vendor.new(params[:vendor])
   if @vendor.save
     if (internet is connected )
       flash[:success] = "Vendor Added Successfully mail have been send"
       VendorMailer.registration_confirmation(@vendor).deliver
       redirect_to amain_path
     else
       flash[:success] = "Vendor Added Successfully mail is not send"
       redirect_to amain_path
     end
   else
     render 'new'
   end
 end

I t would be kind if you help me.

Was it helpful?

Solution

I don't fancy that you check for a connection. As you said, when there is no connection to rails and the remote mail server (or dns) rails throws an exception. So you should catch that exception and handle it accordingly.

def create
  @vendor = Vendor.new(params[:vendor])
  if @vendor.save
    begin
      VendorMailer.registration_confirmation(@vendor).deliver
      flash[:success] = "Vendor Added Successfully"
      redirect_to amain_path
    rescue SocketError => e
      flash[:success] = "Vendor Added Successfully mail is not send"
      redirect_to amain_path
    end
  else
    render 'new'
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top