首页 文章

Heroku / devise - 缺少主机链接!请提供:host参数或设置default_url_options [:host]

提问于
浏览
172

我想在heroku上推送我的应用程序 . 我还在开发中 . 我使用可确认模块设计 .

当我尝试使用heroku控制台添加用户时出现此错误:

Missing host to link to! Please provide :host parameter or set default_url_options[:host]

在测试和开发环境中,我有以下行:

environments / development.rb和environments / 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'

7 回答

  • 11

    我必须做很多事情才能让它在 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 4Devise 3

  • 22

    你需要将它添加到你的 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 .

  • 7

    好,

    首先,您必须使用此命令行安装sendgrid gem:

    heroku addons:add sendgrid:free
    

    然后你只需要像这样配置你的env / dev.rb和env / prod.rb:

    ENV / dev.rb

    config.action_mailer.default_url_options = { :host => 'localhost:3000' }
    

    ENV / prod.rb

    config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
    

    推上git和heroku . 它应该工作..

  • 36

    Codeglot上面的anwser完成了这项工作,但我们想要更灵活的东西,所以我们这样做了:

    在Heroku上,我们运行多个 生产环境 环境进行分段和测试,因此我们需要一个灵活的production.rb环境文件解决方案 .

    在production.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
    
  • 228

    这是一个需要考虑的技巧 . 它将更容易切换服务器和环境,并在heroku的自定义域中更改域 .

    而不是硬编码主机名,而是从请求中读取它 . 这是我的一个简单应用程序的示例 .

    class MyMailController < ApplicationController
      before_filter :set_host_from_request, only: [:create]
    
      ....   
    
      private
      def set_host_from_request
        ActionMailer::Base.default_url_options = { host: request.host_with_port }
      end
    end
    

    在简单示例中,我只有一个动作,即创建,导致发送电子邮件 . 您可以在application_controller.rb中添加before_filter而不使用排除项,以使其始终存储主机名 .

    PRO:

    • 始终在您发送的电子邮件的URL中获取正确的主机名

    • 将我们的登台服务器上的default_url_options配置为 生产环境 ,导致电子邮件被发送给测试用户,并带有 生产环境 链接(当然他们点击它们) . 没有损坏,但非常耗时 .

    CON:

    如果没有default_url_options,则无法在控制台中手动发送

    #config.action_mailer.default_url_options = { :host => 'mydomain.com' }
    $rails console
      User.invite!(email: "ceo@example.com")
    ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
    ...stacktrace
    

    如果您能看到我无法解决的弊端,请分享!谢谢

  • 0

    经过这么多研究后的工作,

    • 不要忘记在 ApplicationMailer (application_mailer.rb) 中添加默认 from: mail 地址,
    class ApplicationMailer < ActionMailer::Base
      default from: 'yourmail@gmail.com'
      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:            'yourmail@gmail.com',
      password:             'yourgmailpassword',
      authentication:       'login',
      enable_starttls_auto: true
    }
    

    你现在好了 . :)

  • 18

    If you're running on 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' }
    

相关问题