Heroku/Rails应用程序错误
我真的被困在这里,担心我的邮件配置文件都搞砸了。我的应用似乎在本地很好地工作,我在Nitrous上。我可以推动heroku大师罚款,当打开可以查看主页,这只是一种形式(https://nameless-taiga-1654.herokuapp.com/),但是一旦我点击提交,我收到一条错误消息。我相信我的所有邮件行为都是正确设置的,我打算通过heroku使用sendgrid来运行邮件的事情。基本上我只是迷路了,需要帮助。附件是表单错误的heroku日志。Heroku/Rails应用程序错误
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"=>"[email protected]", "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 [email protected] (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
型号/ 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
邮寄/ contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: '[email protected]'
def contact_email(contact)
@contact = contact
mail(from: @contact.email, subject: 'Contact Form Message').deliver
end
end
米
的意见/ 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">×</span></button>
<%= value %>
</div>
<% end %>
<%= yield %>
</div>
</body>
</html>
@Pavan @alexsmn我希望这可以帮助?
配置/环境/ 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
}
尝试设置在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
。
我和我目前的项目有同样的问题。事实上,正如我发现的那样,这是一个Gmail身份验证问题。您的应用正尝试登录Gmail帐户,但无法执行此操作,因为Gmail未查看或识别加密的电子邮件和密码。我通过编写必要的值到我smtp_settings直接,就像暂时解决了这个问题:
smtp_settings = {
...
:user_name => "...your gmail email...",
:password => "...your gmail password...",
...
}
另外,我改变了Gmail帐户的安全设置:关闭双重认证,并允许通过安全性较低的应用程序来访问我的帐户。所以,目前该应用程序已部署在Heroku上并正在运行。然而,从网络安全的角度来看,我的解决方案相当危险,所以我仍然在寻找更好的解决方案。
EDITED !!! 有一个更好的方法来加密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'],
...
}
最后,我通过添加两个新变量GMAIL_NAME和GMAIL_PASSWORD以及必要的值,在Heroku:Heroku/Personal apps/myApp/Settings配置回购设置,“配置变量”部分。
它工作。
[Errno :: ECONNREFUSED:连接被拒绝 - 连接(2)用于动作邮件程序](http://*.com/questions/17141004/errnoeconnrefused-connection-refused-connect2-for-action-mailer) –
检查你的'config.action_mailer.smtp_settings'生产。 – alexsmn
@alexsmn我刚刚将设置文件复制到原始问题中。我真的迷失在这里,太令人沮丧了! – RuNpiXelruN