【问题标题】:Errno::ECONNREFUSED in OrdersController#createOrdersController#create 中的 Errno::ECONNREFUSED
【发布时间】:2011-08-11 22:35:31
【问题描述】:

好的,Rails 菜鸟问一个问题。我第一次尝试在这里做 Rails。我正在阅读 Agile Web Dev with Rails 第 4 版。我的生产箱出现此错误。 这在 webrick 下的开发模式下工作,我收到一封电子邮件发送到我的 gmail 帐户和 evrything 但在生产模式下的 apache 盒子上我收到此错误...

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

应用程序跟踪是...

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

这里是 app/controllers/order_controller.rb 中的 def create

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

我的第 58 行和第 54 行有什么问题?这是否与我在 app/config/environment.rb 中的 action_mailer 设置有关?

这里是 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  => "myemail@gmail.com",
    :password   => "<password>",
    :enable_starttls_auto => true
}
end

感谢任何帮助。谢谢。

【问题讨论】:

  • 您的应用似乎由于某种原因无法连接到 gmail。也许考虑使用SendGrid 之类的替代服务
  • 第 54 行没有问题,它只是该代码块的入口点。如果您注释掉第 58 行,异常会消失吗?最好尝试使用debugger
  • 否定注释掉第 58 行不会改变事情。
  • 注释掉第 58 行应该使错误停止,当然这只是为了确认连接到 gmail 时出错。如果您在生产环境中执行此操作,请记住您需要重新启动您的应用程序,因为代码不会像在开发环境中那样自动重新加载。如果您仍有问题,请在此处提供帮助。

标签: ruby-on-rails ruby


【解决方案1】:

我在看书的时候也遇到了同样的问题,但是下面的配置让它工作了:

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

请注意,我没有smtp_settings 哈希中包含域。

最后一件事,请务必在进行更改后重新启动 Rails 应用程序。这将为您省去一些不必要的麻烦。

【讨论】:

    【解决方案2】:

    声誉太低,无法发表评论,但在 @Edgar 的回答中,还要确保如果您正在阅读 smtp 设置的 YAML 配置,则使用 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 不检查字符串键,天哪,那是一个漫长的 3 小时...

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 2012-09-04
      • 2012-04-01
      相关资源
      最近更新 更多