我正在尝试设置一个仅支持Rails 5 API的应用程序,该应用程序接收带有表单数据的POST请求,并将该信息通过电子邮件发送到特定的电子邮件。我基本上是尝试使用联系表单并尝试通过电子邮件将该消息发送给我。
我设置了一个仅限Rails 5 API的应用程序,并使所有内容在本地服务器上运行。但是当我将它部署到heroku时,应用程序一直在崩溃。这是日志: -
2017-10-21T12:31:49.779066+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 28435 -e production`
2017-10-21T12:31:53.419975+00:00 app[web.1]: => Run `rails server -h` for more startup options
2017-10-21T12:31:53.419961+00:00 app[web.1]: => Booting Puma
2017-10-21T12:31:53.419975+00:00 app[web.1]: => Rails 5.1.4 application starting in production
2017-10-21T12:31:53.419976+00:00 app[web.1]: Puma starting in single mode...
2017-10-21T12:31:53.419981+00:00 app[web.1]: * Version 3.10.0 (ruby 2.3.4-p301), codename: Russell's Teapot
2017-10-21T12:31:53.419982+00:00 app[web.1]: * Min threads: 5, max threads: 5
2017-10-21T12:31:53.419982+00:00 app[web.1]: * Environment: production
2017-10-21T12:31:53.568829+00:00 heroku[web.1]: Process exited with status 1
2017-10-21T12:31:53.420126+00:00 app[web.1]: * Listening on tcp://******:28435
2017-10-21T12:31:53.463851+00:00 app[web.1]: Exiting
2017-10-21T12:31:53.465644+00:00 app[web.1]: /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `initialize': getaddrinfo: Name or service not known (SocketError)
2017-10-21T12:31:53.465656+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `new'
2017-10-21T12:31:53.465664+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:105:in `block in parse'
2017-10-21T12:31:53.465663+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:270:in `add_tcp_listener'
2017-10-21T12:31:53.465665+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:88:in `each'
2017-10-21T12:31:53.465665+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/binder.rb:88:in `parse'
2017-10-21T12:31:53.465666+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/runner.rb:144:in `load_and_bind'
2017-10-21T12:31:53.465667+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/single.rb:87:in `run'
2017-10-21T12:31:53.465667+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/puma/launcher.rb:183:in `run'
2017-10-21T12:31:53.465668+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/puma-3.10.0/lib/rack/handler/puma.rb:69:in `run'
2017-10-21T12:31:53.465669+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/rack-2.0.3/lib/rack/server.rb:297:in `start'
2017-10-21T12:31:53.465679+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:44:in `start'
2017-10-21T12:31:53.465680+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:135:in `block in perform'
2017-10-21T12:31:53.465680+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `tap'
2017-10-21T12:31:53.465681+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands/server/server_command.rb:130:in `perform'
2017-10-21T12:31:53.465681+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in `run'
2017-10-21T12:31:53.465682+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in `invoke_command'
2017-10-21T12:31:53.465683+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in `dispatch'
2017-10-21T12:31:53.465687+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/command.rb:44:in `invoke'
2017-10-21T12:31:53.465686+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/command/base.rb:63:in `perform'
2017-10-21T12:31:53.465687+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.3.0/gems/railties-5.1.4/lib/rails/commands.rb:16:in `<top (required)>'
2017-10-21T12:31:53.465688+00:00 app[web.1]: from bin/rails:9:in `require'
2017-10-21T12:31:53.465689+00:00 app[web.1]: from bin/rails:9:in `<main>'
2017-10-21T12:31:53.582296+00:00 heroku[web.1]: State changed from starting to crashed
2017-10-21T12:32:04.663844+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=****** request_id=ae664dd1-a48e-4033-9533-79464373cba8 fwd="157.50.15.44" dyno= connect= service= status=503 bytes= protocol=https
2017-10-21T12:32:06.597178+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=****** request_id=48a773e2-e949-4820-b415-42a3071f79b9 fwd="157.50.15.44" dyno= connect= service= status=503 bytes= protocol=https
我遇到了这个特定错误“getaddrinfo:名称或服务未知(SocketError)”。我查了一下,发现有几个消息来源建议在初始化器中添加'require resolv-replace'。我做了,没有变化。该应用程序不断崩溃。
我必须提一下,我从我工作的Rails 5(非API)应用程序复制粘贴的代码,并使用适当的控制器方法在JSON中进行渲染。
正如我所提到的,我让它在本地服务器上工作,所以它与heroku和我在CLI上通过命令添加的sendgrid插件有关。
这是代码: -
class ContactsController < ApplicationController
def create
@message = Message.new(message_params)
if @message.valid?
ContactMailer.contact(@message).deliver_now
render json: {status: 'SUCCESS', message: 'Message sent successfully!'}, status: :ok
else
render json: {status: 'ERROR', message: 'Message sending failed!', data: @message.errors}, status: :unprocessable_entity
end
end
private
def message_params
params.permit(:subject, :name,
:email, :content)
end
end
添加到production.rb的行
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
host = ENV['HOST']
config.action_mailer.default_url_options = { host: host }
ActionMailer::Base.smtp_settings = {
:port => ENV['SMTP_PORT'],
:address => ENV['SMTP_ADDRESS'],
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
环境变量通过在线设置页面添加到heroku配置变量中。
如果有任何其他信息有用,请告诉我。