【问题标题】:Heroku/devise - Missing host to link to! Please provide :host parameter or set default_url_options[:host]Heroku/devise - 缺少要链接的主机!请提供 :host 参数或设置 default_url_options[:host]
【发布时间】:2011-05-06 02:47:46
【问题描述】:

我正在尝试在 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

我像这样配置 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'

【问题讨论】:

    标签: ruby-on-rails heroku devise actionmailer


    【解决方案1】:

    您需要将此添加到您的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

    【讨论】:

    • 嗨,山姆!我很抱歉,但我没有得到它。我只是在我的 env.rb 中添加了 config.action_mailer.default_url_options = { :host => 'localhost' } 行,但是我的 rails 3 应用程序在 heroku 上崩溃了。我是否也必须在 env/prod.rb 上放一些东西? (对不起,我的英语太一般了..)
    • 另外,我是否必须将我的 Rails 应用程序置于生产模式?谢谢!
    • :host => 'yourapp.heroku.com' 这应该是您的域。例如,如果您的域是 yahoo.com,那么它应该是 yahoo.heroku.com,或者如果您已经设置了自定义域,它只是您的 url yahoo.com
    • 谢谢 Sam,我找到了解决方案。事实是我没有 sendgrid 插件,我的 smtp 也没有配置。非常感谢您的所有备份!
    • 不是config.action_mailer.default_url_options = { :host => 'yourapp.herokuapp.com',而不是config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' 吗?
    【解决方案2】:

    好的,

    首先你必须使用这个命令行安装 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。它应该工作..

    【讨论】:

    • 当我在 production.rb 中引用 config.action_mailer 时出现此错误:/app/.bundle/gems/ruby/1.9.1/gems/railties-3.0.7/lib/rails/railtie /configuration.rb:77:in method_missing': undefined method action_mailer' for # (NoMethodError)
    • 我不确定我能不能在这里提供帮助...您在块内引用它了吗? (Website::Application.configure do ... config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' } ... end)
    • 现在 sendgrid 命令是 heroku addons:add sendgrid:starter (查看使用详情devcenter.heroku.com/articles/sendgrid)
    【解决方案3】:

    上面的 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
    

    【讨论】:

      【解决方案4】:

      如果您在 Cedar 上运行:

      1. 从您的控制台运行 heroku addons:add sendgrid:free

      2. 将以下行添加到您应用中的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' }
      

      【讨论】:

        【解决方案5】:

        我必须做很多事情才能让它在生产环境中工作: 在我的 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

        【讨论】:

        • default_url_options 的这种结构导致了一个错误,导致我的预编译资产失败,仅供参考。当我把它改回 ` = { host: 'myappsname.herokuapp.com' }` 时,它起作用了。
        【解决方案6】:

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

        1. 不要忘记在您的 ApplicationMailer (application_mailer.rb) 中添加默认 from: mail 地址,

          class ApplicationMailer < ActionMailer::Base
            default from: 'yourmail@gmail.com'
            layout 'mailer'
          end
          
        2. 在您的 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
          }
          
        3. 转发 IMAP/POP 选项卡的 Gmail 设置中启用 IMAP。

        4. 允许不太安全的应用:开启来自https://myaccount.google.com/lesssecureapps

        您现在可以开始了。 :)

        【讨论】:

          猜你喜欢
          • 2019-12-11
          • 1970-01-01
          • 2017-12-31
          • 1970-01-01
          • 2014-04-12
          • 1970-01-01
          • 2016-07-08
          • 1970-01-01
          • 2018-11-08
          相关资源
          最近更新 更多