我正在尝试将我的应用程序推送到 heroku 上。我还在开发中。我将设计与可确认模块一起使用。
当我尝试使用 heroku 控制台添加用户时,出现此错误:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
在测试和开发环境中,我有以下行:
环境/development.rb 和环境/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
我没有在生产环境中设置任何东西。
我试图推动
config.action_mailer.default_url_options = { :host => 'mywebsitename.com' }
config.action_mailer.default_url_options = { :host => 'heroku.mywebsitename.com' }
但它也不起作用..
我在网上看到它可能与 ActionMailer 有关,但我不知道我必须配置什么。非常感谢你的想法!
编辑:
你好,
为了不让我的应用程序在我推送 heroku 时崩溃,我把它放在我的 env/test.rb 和我的 env/dev.rb 中(不是在 env.rb 我认为这是因为它是一个 rails 3 应用程序)
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
但是当我尝试在 heroku 控制台中创建用户时:
User.create(:username => "test", :email => "test@test.com", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")
这是我得到的错误:
ActionView::Template::Error: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:473:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:195:in `user_confirmation_url'
已编辑 (2)
当我在控制台上输入 heroku 日志时,我得到了这个 ==> production.log <== 所以我认为当一个部署在 heroku 上时,它已经在生产中了。
我像这样配置 env/prod.rb:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
现在当我尝试创建用户时出现此错误:
Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `open'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/ruby1.8.7/lib/ruby/1.8/timeout.rb:62:in `timeout'
您需要将此添加到您的 environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
确保将 host
更改为您的生产 url 并将其保留为 localhost 以进行开发。这是给邮寄者的,它需要一个默认的电子邮件来发送诸如确认等通知......
您应该检查从控制台运行的 heroku 服务器 heroku logs
上的日志,它会告诉您确切的错误。
当您推送到 heroku 时,您需要使用 heroku 子域配置 environment.rb
文件:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
根据版本,这应该放在 production.rb
,而不是 environment.rb
。
好的,
首先,您必须使用以下命令行安装 sendgrid gem:
heroku addons:add sendgrid:free
然后你只需要像这样配置你的 env/dev.rb 和 env/prod.rb :
环境/dev.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
环境/prod.rb
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
推动 git 和 heroku。它应该工作..
method_missing': undefined method
action_mailer' (无方法错误)
上面的 Codeglot 的 anwser 完成了这项工作,但我们想要更灵活的东西,所以我们这样做了:
在 Heroku 上,我们运行多个生产环境进行登台和测试,因此我们需要一个灵活的解决方案来处理 production.rb 环境文件。
在生产中.rb
config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }
然后像这样为您的应用设置 MAILER_URL 环境变量
heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app
如果您在 Cedar 上运行:
从您的控制台运行 heroku addons:add sendgrid:free。将以下行添加到应用程序的 config/environments/production.rb 中。
.
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'YOUR-DOMAIN-HERE.COM' }
我必须做很多事情才能让它在 production 环境中工作:在我的 production.rb
文件 (/config/environments/production.rb) 中,我添加了以下内容:
Rails.application.routes.default_url_options[:host] = 'myappsname.herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
这是 Rails 4 和 Devise 3
default_url_options
的这种结构导致了一个错误,导致我的预编译资产失败,仅供参考。当我将它改回 ` = { host: 'myappsname.herokuapp.com' }` 时,它起作用了。
经过这么多研究后的工作,
不要忘记在您的 ApplicationMailer (application_mailer.rb) 中添加 default from: mail address as, class ApplicationMailer < ActionMailer::Base default from: 'yourmail@gmail.com' layout 'mailer' end 在您的生产中添加以下配置.rb。 config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com' } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { 地址:'smtp.gmail.com',端口:587,域: 'heroku.com', user_name: 'yourmail@gmail.com', password: 'yourgmailpassword', authentication: 'login', enable_starttls_auto: true } 从转发 IMAP/POP 选项卡中的 Gmail 设置启用 IMAP。允许不太安全的应用程序:从 https://myaccount.google.com/lesssecureapps 开启
你现在可以走了。 :)
不定期副业成功案例分享
:host => 'yourapp.heroku.com'
这应该是您的域。例如,如果您的域是 yahoo.com,那么它应该是yahoo.heroku.com
,或者如果您已经设置了自定义域,它将只是您的 urlyahoo.com
config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com'
,而不仅仅是config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com'
吗?