Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errno::ECONNREFUSED in OrdersController#create

OK, Rails noob asking a question. I'm trying to do Rails for the first time here. I'm reading Agile Web Dev with Rails 4th ed. I'm getting this error on my production box. This works in development mode under webrick, I get an email sent to my gmail acount and evrything but on my apache box in production mode I get this error...

Errno::ECONNREFUSED in OrdersController#create
Connection refused - connect(2)

Application trace is...

app/controllers/orders_controller.rb:58:in `create'
app/controllers/orders_controller.rb:54:in `create'

And here is def create in app/controllers/order_controller.rb

def create
@order = Order.new(params[:order])
@order.add_line_items_from_cart(current_cart)

respond_to do |format|  #THIS IS LINE 54
  if @order.save
    Cart.destroy(session[:cart_id]) 
    session[:cart_id] = nil 
    Notifier.order_received(@order).deliver     #THIS IS LINE 58
    format.html { redirect_to(store_url, :notice => I18n.t('.thanks')) }
    format.xml  { render :xml => @order, :status => :created, :location => @order }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @order.errors, :status => :unprocessable_entity }
  end
end

What's wrong with my line 58 and 54? Does this have to do with my action_mailer settings in app/config/environment.rb?

Here is environment.rb

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
Depot::Application.initialize!

#uncertain about anything below this line

Depot::Application.configure do 
  config.action_mailer.delivery_method = :smtp
  config.action_mailer.smtp_settings = {
    :address    => "smtp.gmail.com",
    :port       => 587,
    :domain     => "gmail.com",
    :authentication => "plain",
    :user_name  => "[email protected]",
    :password   => "<password>",
    :enable_starttls_auto => true
}
end

Any help is appreciated. Thanks.

like image 615
thefonso Avatar asked Aug 11 '11 22:08

thefonso


2 Answers

I had this same issue when reading the book, but the following configuration made it work:

config.action_mailer.smtp_settings = {
  enable_starttls_auto: true,
  address: 'smtp.gmail.com',
  port: 587,
  authentication: 'plain',
  user_name: '[email protected]',
  password: '<password>'
}

Note that I didn't include the domain on the smtp_settings hash.

One last thing, be sure to restart your rails application after you make the changes. That will save you some unnecessary headaches.

like image 96
Edgar Gonzalez Avatar answered Sep 23 '22 07:09

Edgar Gonzalez


Reputation too low to comment, but in @Edgar's answer, also make sure that if you are reading in a YAML config for the smtp settings, you use symbolize_keys!:

email_settings = YAML::load(File.open("#{Rails.root.to_s}/config/email.yml"))
config.action_mailer.smtp_settings = email_settings[Rails.env].symbolize_keys! unless email_settings[Rails.env].nil?

Rails doesn't check for string keys, and goodness, that was a long 3 hours...

like image 30
Tim Avatar answered Sep 23 '22 07:09

Tim