【问题标题】:Heroku/Rails App ErrorHeroku/Rails 应用程序错误
【发布时间】:2015-10-31 15:00:48
【问题描述】:

我真的被困在这里,担心我的邮件配置文件都搞砸了。我的应用程序似乎在本地运行良好,我在 Nitrous 上。我可以很好地 git push heroku master,打开后可以查看主页,这只是一个表单(https://nameless-taiga-1654.herokuapp.com/),但是一旦我点击提交,我就会收到一条错误消息。我相信我在 nitrous 上的所有邮件操作都设置正确,我计划通过 heroku 使用 sendgrid 来运行邮件方面的事情。基本上我只是迷路了,需要帮助。附上表单错误的heroku日志。

heroku 日志 -t

2015-08-07T11:00:54.501756+00:00 heroku[router]: at=info method=POST path="/contacts" host=nameless-taiga-1654.herokuapp.com request_id=452df9b2-aef8-4a23-9ffb-3ba6781d80
75 fwd="124.149.33.78" dyno=web.1 connect=1ms service=72ms status=500 bytes=1754
2015-08-07T11:00:54.433019+00:00 app[web.1]: Started POST "/contacts" for 124.149.33.78 at 2015-08-07 11:00:54 +0000
2015-08-07T11:00:54.437794+00:00 app[web.1]:   Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z2Qh/GDIycId0uJk2evhblbKisGXNlIzjtz+xfJoyac=", "contact"=>{"name"=>"John D
oe", "phone"=>"040000000", "email"=>"johndoe@gmail.com", "event_type"=>"Corporate", "comments"=>"Test Comments"}, "commit"=>"Submit"}
2015-08-07T11:00:54.491841+00:00 app[web.1]:
2015-08-07T11:00:54.491846+00:00 app[web.1]: Sent mail to justindavidson23@gmail.com (21.8ms)
2015-08-07T11:00:54.491848+00:00 app[web.1]: Completed 500 Internal Server Error in 53ms
2015-08-07T11:00:54.492821+00:00 app[web.1]:
2015-08-07T11:00:54.492824+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - connect(2)):
2015-08-07T11:00:54.436340+00:00 app[web.1]: Processing by ContactsController#create as HTML
2015-08-07T11:00:54.492826+00:00 app[web.1]:   app/mailers/contact_mailer.rb:8:in `contact_email'
2015-08-07T11:00:54.463333+00:00 app[web.1]:   Rendered contact_mailer/contact_email.html.erb (0.3ms)
2015-08-07T11:00:54.492828+00:00 app[web.1]:   app/controllers/contacts_controller.rb:9:in `create'
2015-08-07T11:00:54.492831+00:00 app[web.1]:
2015-08-07T11:00:54.492830+00:00 app[web.1]:
2015-08-07T11:00:54.492827+00:00 app[web.1]:   app/models/contact.rb:7:in `send_email'

contacts_controller.rb

class ContactsController < ApplicationController
  def new
    @contact = Contact.new
  end

  def create
    @contact = Contact.new(contact_params)

    if @contact.save
      flash[:success] = 'Message Sent.'
      redirect_to new_contact_path
    else
      flash[:danger] = 'Error occurred, messgage not sent.'
      redirect_to new_contact_path
    end
  end
end

models/contact.rb

class Contact < ActiveRecord::Base
  validates :name, :email, presence: true
  after_create :send_email

  private
  def send_email
    ContactMailer.contact_email(self).deliver
  end
end

mailers/contact_mailer.rb

class ContactMailer < ActionMailer::Base
  default to: 'justindavidson23@gmail.com'


  def contact_email(contact)
   @contact = contact

    mail(from: @contact.email, subject: 'Contact Form Message').deliver
  end
end

views/contact_mailer/contact_email.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
  <body>
    <p>New Message from Hoot and Holla's Contact form, from <%= "#{@contact.name}, #{@contact.email}" %></p>
    <p><%= @contact.phone %></p>
    <p><%= @contact.event_type %></p>
    <p><%= @contact.comments %></p>
  </body>
</html>

application.html.erb

<!DOCTYPE html>
<html>
<head>
  <title>HootandhollaLp</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

  <div class="container">

    <% flash.each do |key, value| %>
      <div class="alert alert-<%= key %> alert-dismissible">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <%= value %>
     </div>
    <% end %>

    <%= yield %>
  </div>

</body>
</html>

@Pavan @alexsmn 我希望这会有所帮助?


config/environments/production.rb

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # Code is not reloaded between requests.
  config.cache_classes = true

  # Eager load code on boot. This eager loads most of Rails and
  # your application in memory, allowing both threaded web servers
  # and those relying on copy on write to perform better.
  # Rake tasks automatically ignore this option for performance.
  config.eager_load = true

  # Full error reports are disabled and caching is turned on.
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true

  # Enable Rack::Cache to put a simple HTTP cache in front of your application
  # Add `rack-cache` to your Gemfile before enabling this.
  # For large-scale production use, consider using a caching reverse proxy like nginx, varnish or squid.
  # config.action_dispatch.rack_cache = true

  # Disable Rails's static asset server (Apache or nginx will already do this).
  config.serve_static_assets = false

  # Compress JavaScripts and CSS.
  config.assets.js_compressor = :uglifier
  # config.assets.css_compressor = :sass

  # Do not fallback to assets pipeline if a precompiled asset is missed.
  config.assets.compile = false

  # Generate digests for assets URLs.
  config.assets.digest = true

  # Version of your assets, change this if you want to expire all your assets.
  config.assets.version = '1.0'

  # Specifies the header that your server uses for sending files.
  # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
  # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  # config.force_ssl = true

  # Set to :debug to see everything in the log.
  config.log_level = :info

  # Prepend all log lines with the following tags.
  # config.log_tags = [ :subdomain, :uuid ]

  # Use a different logger for distributed setups.
  # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)

  # Use a different cache store in production.
  # config.cache_store = :mem_cache_store

  # Enable serving of images, stylesheets, and JavaScripts from an asset server.
  # config.action_controller.asset_host = "http://assets.example.com"

  # Precompile additional assets.
  # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
  # config.assets.precompile += %w( search.js )

  # Ignore bad email addresses and do not raise email delivery errors.
  # Set this to true and configure the email server for immediate delivery to raise delivery errors.
  # config.action_mailer.raise_delivery_errors = false

  # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
  # the I18n.default_locale when a translation cannot be found).
  config.i18n.fallbacks = true

  # Send deprecation notices to registered listeners.
  config.active_support.deprecation = :notify

  # Disable automatic flushing of the log to improve performance.
  # config.autoflush_log = false

  # Use default logging formatter so that PID and timestamp are not suppressed.
  config.log_formatter = ::Logger::Formatter.new

  # Do not dump schema after migrations.
  config.active_record.dump_schema_after_migration = false
end

config/environment.rb

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

# Initialize the Rails application.
Rails.application.initialize!

ActionMailer::Base.smtp_settings = {
  :address => 'smtp.sendgrid.net',
  :port => '587',
  :authentication => :plain,
  :user_name => ENV['SENDGRID_USERNAME'],
  :password => ENV['SENDGRID_PASSWORD'],
  :domain => 'heroku.com',
  :enable_startstls_auto => true 
  }

【问题讨论】:

标签: ruby-on-rails git ruby-on-rails-4 heroku


【解决方案1】:

尝试在您的 production.rb 文件中设置以下内容:

config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.default_url_options = { host: 'nameless-taiga-1654.herokuapp.com' }

另外,要验证配置是否正确,您可以运行heroku config

【讨论】:

    【解决方案2】:

    我在处理当前项目时遇到了同样的问题。实际上,正如我发现的,这是一个 Gmail 身份验证问题。您的应用正在尝试登录 Gmail 帐户,但无法登录,因为 Gmail 看不到或识别加密的电子邮件和密码。我通过将必要的值直接写入我的 smtp_settings 暂时解决了这个问题,例如:

        smtp_settings = {
          ...
          :user_name => "...your gmail email...",
          :password => "...your gmail password...",
          ...
        }
    

    另外,我更改了 Gmail 帐户安全设置:关闭双重身份验证并允许不太安全的应用访问我的帐户。因此,目前该应用程序已部署在 Heroku 上并且正在运行。但是,从网络安全的角度来看,我的解决方案相当危险,所以我仍在寻找更好的解决方案。

    已编辑!!! 有一种更好的方法来加密 gmail 帐户设置。在我的 .bashrc 文件中(隐藏在计算机根目录中 - 按 Ctrl+H 显示)我编写了以下代码行:

        export GMAIL_NAME=necessary value here
        export GMAIL_PASSWORD=necessary value here
    

    在此之后,我还再次更改了我的 smtp_settings,例如:

        smtp_settings = {
          ...
          user_name: ENV['GMAIL_NAME'],
          password: ENV['GMAIL_PASSWORD'],
          ...
        } 
    

    最后,我在 Heroku 上配置了 repo 设置:Heroku/Personal apps/myApp/Settings,通过添加两个具有必要值的新变量 GMAIL_NAME 和 GMAIL_PASSWORD 部分的“配置变量”。

    成功了。

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2014-11-19
      • 2014-03-12
      • 2013-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多