Ruby-on-rails Heroku/devise - 缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4114835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]
提问by benoitr
I am trying to push my app on heroku. I am still in dev. I use devise with the confirmable module.
我正在尝试将我的应用程序推送到 heroku。我还在开发中。我使用带有可确认模块的设计。
When I try to add a user with the heroku console I got this error:
当我尝试使用 heroku 控制台添加用户时,出现此错误:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
in test and dev environment i have the following line:
在测试和开发环境中,我有以下几行:
environments/development.rb and environments/test.rb
环境/development.rb 和环境/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I don't have set up something in the production environment.
我没有在生产环境中设置一些东西。
I've tried to push with
我试图推动
config.action_mailer.default_url_options = { :host => 'mywebsitename.com' }
config.action_mailer.default_url_options = { :host => 'heroku.mywebsitename.com' }
but it doesn't work too..
但它也不起作用..
I see on the web that it could be related to ActionMailer but I don't know what I have to configure. Many thanks for your idea!
我在网上看到它可能与 ActionMailer 有关,但我不知道我必须配置什么。非常感谢您的想法!
EDITED:
编辑:
Hi,
你好,
In order to not make my app crashes when I push on heroku I put this in my env/test.rb and my env/dev.rb (not in env.rb I think it is because it's a rails 3 app)
为了不让我的应用程序在我按下 heroku 时崩溃,我把它放在我的 env/test.rb 和我的 env/dev.rb (不是在 env.rb 我认为这是因为它是一个 rails 3 应用程序)
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
But when I tried to create a user in the heroku console:
但是当我尝试在 heroku 控制台中创建用户时:
User.create(:username => "test", :email => "[email protected]", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")
here are errors I got:
这是我得到的错误:
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'
EDITED (2)
已编辑 (2)
When I type heroku logs on console I got this ==> production.log <== So I think when one deploys on heroku it's already in production.
当我在控制台上输入 heroku 日志时,我得到了这个 ==> production.log <== 所以我认为当在 heroku 上部署时它已经在生产中了。
I configure the env/prod.rb like this:
我像这样配置 env/prod.rb:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
and now I have this as error when I try to create a User:
现在,当我尝试创建用户时,我将此作为错误:
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'
回答by thenengah
You need to add this to your environment.rb
您需要将此添加到您的 environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
Make sure you change hostto your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...
确保更改host为生产 url 并将其保留为 localhost 以进行开发。这是给邮寄者的,它需要一个默认的电子邮件来发送确认等通知......
You should check the logs on the heroku server heroku logsrun that from the console and it will tell you the exact error.
您应该检查heroku logs从控制台运行的 heroku 服务器上的日志,它会告诉您确切的错误。
When you push to heroku you need to configure the environment.rbfile with the heroku subdomain:
当您推送到 heroku 时,您需要environment.rb使用 heroku 子域配置文件:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Depending upon version, this should go in production.rb, not environment.rb.
根据版本,这应该进入production.rb,而不是environment.rb.
回答by benoitr
Ok,
好的,
First you have to install the sendgrid gem with this command line:
首先,您必须使用以下命令行安装 sendgrid gem:
heroku addons:add sendgrid:free
Then you just have to configure your env/dev.rb and env/prod.rb like this:
然后你只需要像这样配置你的 env/dev.rb 和 env/prod.rb :
env/dev.rb
环境/开发.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
env/prod.rb
环境/产品.rb
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Push on git and heroku. It should work..
推动 git 和 heroku。它应该工作..
回答by Ross
Codeglot's anwser above does the job, but we wanted something a bit more flexible, so we did this:
上面的 Codeglot 的 anwser 可以完成这项工作,但我们想要更灵活的东西,所以我们这样做了:
On Heroku, we run multiple Production environments for staging and testing, so we need a flexible solution for the production.rb environment file.
在 Heroku 上,我们运行多个 Production 环境来进行登台和测试,因此我们需要一个灵活的 production.rb 环境文件解决方案。
In production.rb
在生产.rb
config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }
Then set the MAILER_URL environment variable for your app like so
然后像这样为您的应用程序设置 MAILER_URL 环境变量
heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app
回答by Mike
If you're running on Cedar:
如果您在Cedar 上运行:
run
heroku addons:add sendgrid:freefrom your console.Add the following lines to
config/environments/production.rbin your app.
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' }
回答by maudulus
I had to do a number of things to get it to work in the productionenvironment:
Inside of my production.rbfile (/config/environments/production.rb) I added the following:
我必须做很多事情才能让它在生产环境中工作:在我的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"
This is with Rails 4and Devise 3
这是Rails 4和Devise 3
回答by Anshab M K
The working one after so many research,
经过这么多研究后的工作,
Don't forget to add default from: mailaddress in your ApplicationMailer (application_mailer.rb)as,
class ApplicationMailer < ActionMailer::Base default from: '[email protected]' layout 'mailer' endAdd the below configuration in your production.rb.
config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com' } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'heroku.com', user_name: '[email protected]', password: 'yourgmailpassword', authentication: 'login', enable_starttls_auto: true }Enable IMAP from your Gmail settings in Forwarding IMAP/POPtab.
Allow less secure apps: ONfrom https://myaccount.google.com/lesssecureapps
不要忘记在您的ApplicationMailer (application_mailer.rb) 中添加 default from: mailaddress as,
class ApplicationMailer < ActionMailer::Base default from: '[email protected]' layout 'mailer' end在您的production.rb 中添加以下配置。
config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com' } config.action_mailer.delivery_method = :smtp config.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'heroku.com', user_name: '[email protected]', password: 'yourgmailpassword', authentication: 'login', enable_starttls_auto: true }从您的 Gmail 设置中的转发 IMAP/POP选项卡启用 IMAP 。
允许不太安全的应用程序:从https://myaccount.google.com/lesssecureapps打开
You're now good to go. :)
你现在可以走了。:)

