我刚刚将我的Ruby应用程序部署到Heroku,而我的contacts_controller.rb中的redirect_to不再有效。如果我运行本地服务器,它工作正常,但在Heroku上,它将我带到一个不存在的URL,所以我收到一条错误消息。
我已经查看了Heroku日志,甚至在那里,它说它正在重定向到正确的URL但是它与应用程序中的内容不匹配。
我试过重启Heroku。我搜索了文档以查看是否有办法手动更改路径,因为我注意到日志中的某一点,它说它正在推送到'/ contacts',这是错误的URL,它给了我错误但我现在认为引用了db,而不是重定向URL。
在if / else方法中,redirect_to路径对于成功或失败是一样的奇怪,它可以正常运行但不成功。
contacts_controller.rb文件:
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
redirect_to new_contact_path
name = params[:contact][:name]
email = params[:contact][:email]
body = params[:contact][:comments]
ContactMailer.contact_email(name, email, body).deliver
flash[:success] = "Message sent."
else
flash[:danger] = @contact.errors.full_messages.join(", ")
redirect_to new_contact_path
end
end
private
def contact_params
params.require(:contact).permit(:name, :email, :comments)
end
end
这是我提交联系表单后的heroku日志:
2017-03-02T12:15:00.498443+00:00 app[web.1]: I, [2017-03-02T12:15:00.498389 #4] INFO -- : [] Redirected to https://myapp.herokuapp.com/contact-us
2017-03-02T12:15:00.506175+00:00 app[web.1]: I, [2017-03-02T12:15:00.506104 #4] INFO -- : [] Rendering contact_mailer/contact_email.html.erb
2017-03-02T12:15:00.506925+00:00 app[web.1]: I, [2017-03-02T12:15:00.506848 #4] INFO -- : [] Rendered contact_mailer/contact_email.html.erb (0.6ms)
2017-03-02T12:15:00.793142+00:00 app[web.1]: I, [2017-03-02T12:15:00.793082 #4] INFO -- : [] Sent mail to xxxxxxxx@xxxx.xxx (6.6ms)
2017-03-02T12:15:00.786369+00:00 app[web.1]: D, [2017-03-02T12:15:00.786255 #4] DEBUG -- : [] ContactMailer#contact_email: processed outbound mail in 286.1ms
2017-03-02T12:15:00.793195+00:00 app[web.1]: D, [2017-03-02T12:15:00.793141 #4] DEBUG -- : [] Date: Thu, 02 Mar 2017 12:15:00 +0000
2017-03-02T12:15:00.793197+00:00 app[web.1]: From: is
2017-03-02T12:15:00.793197+00:00 app[web.1]: To: xxxxx@xxx.xxx
2017-03-02T12:15:00.793198+00:00 app[web.1]: Message-ID: <xxxx.mail>
2017-03-02T12:15:00.793199+00:00 app[web.1]: Subject: Contact Form Message
2017-03-02T12:15:00.793200+00:00 app[web.1]: Mime-Version: 1.0
2017-03-02T12:15:00.793200+00:00 app[web.1]: Content-Type: text/html;
2017-03-02T12:15:00.793201+00:00 app[web.1]: charset=UTF-8
2017-03-02T12:15:00.793201+00:00 app[web.1]: Content-Transfer-Encoding: 7bit
2017-03-02T12:15:00.793202+00:00 app[web.1]: <!DOCTYPE html>
2017-03-02T12:15:00.793202+00:00 app[web.1]:
2017-03-02T12:15:00.793203+00:00 app[web.1]: <html>
2017-03-02T12:15:00.793204+00:00 app[web.1]: <head></head>
2017-03-02T12:15:00.793204+00:00 app[web.1]: <body>
2017-03-02T12:15:00.793205+00:00 app[web.1]: <p>You have received a message from the site's contact form, from
2017-03-02T12:15:00.793206+00:00 app[web.1]: this, is.</p>
2017-03-02T12:15:00.793207+00:00 app[web.1]: <p>frustrating</p>
2017-03-02T12:15:00.793208+00:00 app[web.1]: </body>
2017-03-02T12:15:00.793208+00:00 app[web.1]: </html>
2017-03-02T12:15:00.793409+00:00 app[web.1]: I, [2017-03-02T12:15:00.793333 #4] INFO -- : [] Completed 500 Internal Server Error in 305ms (ActiveRecord: 3.8ms)
2017-03-02T12:15:00.793869+00:00 app[web.1]: F, [2017-03-02T12:15:00.793819 #4] FATAL -- : []
2017-03-02T12:15:00.793910+00:00 app[web.1]: F, [2017-03-02T12:15:00.793871 #4] FATAL -- : [] Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
2017-03-02T12:15:00.793958+00:00 app[web.1]: F, [2017-03-02T12:15:00.793909 #4] FATAL -- : []
2017-03-02T12:15:00.793979+00:00 app[web.1]: F, [2017-03-02T12:15:00.793943 #4] FATAL -- : [] app/controllers/contacts_controller.rb:13:in `create'
2017-03-02T12:15:00.794339+00:00 heroku[router]: at=info method=POST path="/contacts" host=myapp.herokuapp.com request_id=25154829-90fe-44c4-a715-eaf9a6fd83d1 fwd="87.114.208.14" dyno=web.1 connect=1ms service=313ms status=500 bytes=1669
它重定向到我的实际链接是https://myapp.herokuapp.com/contacts
屏幕上显示错误消息:“我们很抱歉,但出了点问题。
如果您是应用程序所有者,请检查日志以获取更多信息。“
我觉得它可能与我的数据库有关,但我不知道为什么它会重定向到该链接。我迁移到Heroku的数据库称为“联系人”
真的很感激任何建议,谢谢。
答案 0 :(得分:0)
您的问题似乎与电子邮件递送有关,而不是重定向。如果您再次查看日志,您将看到以下行:
2017-03-02T12:15:00.793409+00:00 app[web.1]: I, [2017-03-02T12:15:00.793333 #4] INFO -- : [] Completed 500 Internal Server Error in 305ms (ActiveRecord: 3.8ms)
2017-03-02T12:15:00.793869+00:00 app[web.1]: F, [2017-03-02T12:15:00.793819 #4] FATAL -- : []
2017-03-02T12:15:00.793910+00:00 app[web.1]: F, [2017-03-02T12:15:00.793871 #4] FATAL -- : [] Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
您的应用程序返回500
错误,日志中显示的错误消息表明电子邮件传送出现问题(通过SMTP发送电子邮件使用端口25)。
您是否为Heroku帐户安装并配置了电子邮件加载项并附加到您的应用程序? Heroku的网站上列出了几个:https://elements.heroku.com/addons/categories/email-sms
我怀疑这是在本地工作,因为您正在运行postfix
之类的软件,但这在Heroku上并不存在。