【问题标题】:ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):ActionView::Template::Error(缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或将 :only_path 设置为 true):
【发布时间】:2014-09-25 20:35:01
【问题描述】:

当我尝试在生产中为我的应用注册新用户时出现以下错误。我已经阅读了所有其他具有相同错误的 stackoverflow 问题,并且我已经尝试了他们的所有解决方案,但到目前为止没有一个对我有用。

错误:

ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

我在 production.rb 文件中使用我自己的应用名称正确设置了此代码:

config.action_mailer.default_url_options = { host: 'myapp.herokuapp.com' } 

这是完整的 production.rb 文件:

Blocipedia::Application.configure do
  # Settings specified here will take precedence over those in config/application.rb.
  config.cache_classes = true


  config.eager_load = true

  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true


  config.serve_static_assets = false


  config.assets.js_compressor = :uglifier

  config.assets.compile = false


  config.assets.digest = true


  config.assets.version = '1.0'


  config.log_level = :info


  config.i18n.fallbacks = true

  config.active_support.deprecation = :notify


  config.log_formatter = ::Logger::Formatter.new

  config.action_mailer.default_url_options = { host: 'myapp.herokuapp.com' }
end

我仔细检查了我已将 Sendgrid 配置为 Heroku 中的附加组件。我的密钥和密码是正确的。

任何建议将不胜感激!谢谢!

编辑:我正在使用 Devise 进行用户身份验证。我不确定该错误所指的邮件模板文件到底是哪个文件,但我猜它是新的注册视图,然后是与此错误相关的邮件确认说明视图,对吗?他们在这里:

新的注册视图:

<h2>Sign up</h2>

<div class="row">
  <div class="col-md-8">
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= devise_error_messages! %>
      <div class="form-group">
        <%= f.label :name %>
        <%= f.text_field :name, autofocus: true, class: 'form-control', placeholder: "Enter name" %>
      </div>
      <div class="form-group">
        <%= f.label :email %>
        <%= f.email_field :email, class: 'form-control', placeholder: "Enter email" %>
      </div>
      <div class="form-group">
        <%= f.label :password %>
        <%= f.password_field :password, class: 'form-control', placeholder: "Enter password" %>
      </div>
      <div class="form-group">
        <%= f.label :password_confirmation %>
        <%= f.password_field :password_confirmation, class: 'form-control', placeholder: "Enter password confirmation" %>
      </div>
      <div class="form-group">
        <%= f.submit "Sign up", class: 'btn btn-success' %>
      </div>
      <div class="form-group">
        <%= render "devise/shared/links" %>
      </div>
    <% end %>
  </div>
</div>

及确认说明查看:

<p>Welcome <%= @email %>!</p>

<p>You can confirm your account email through the link below:</p>

<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>

这是整个错误跟踪日志:

Rendered devise/registrations/new.html.erb within layouts/application (3.3ms)
2014-09-24T23:38:38.136606+00:00 heroku[router]: at=info method=GET path="/users/sign_up" host=blocipedia.herokuapp.com request_id=f52c96a7-014c-4667-b1b3-93e8b47e4364 fwd="70.197.237.94" dyno=web.1 connect=2ms service=12ms status=304 bytes=850
2014-09-24T23:38:48.250414+00:00 app[web.1]: Started POST "/users" for 70.197.237.94 at 2014-09-24 23:38:48 +0000
2014-09-24T23:38:48.347819+00:00 app[web.1]: Completed 500 Internal Server Error in 95ms
2014-09-24T23:38:48.349962+00:00 app[web.1]: 
2014-09-24T23:38:48.349966+00:00 app[web.1]: 
2014-09-24T23:38:48.349969+00:00 app[web.1]:     2: 
2014-09-24T23:38:48.347813+00:00 app[web.1]: Completed 500 Internal Server Error in 95ms
2014-09-24T23:38:48.349959+00:00 app[web.1]:     5: <p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
2014-09-24T23:38:48.349972+00:00 app[web.1]:     4: 
2014-09-24T23:38:48.349974+00:00 app[web.1]:   app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb___2772878935586456441_70136789435480'
2014-09-24T23:38:48.349977+00:00 app[web.1]: 
2014-09-24T23:38:48.252507+00:00 app[web.1]: Processing by Devise::RegistrationsController#create as HTML
2014-09-24T23:38:48.349953+00:00 app[web.1]: ActionView::Template::Error (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true):

【问题讨论】:

  • 电子邮件是作为后台作业发送还是在请求期间发送?如果是后台作业(例如,通过 Heroku Scheduler 运行 rake 任务),请确保将环境正确设置为生产环境。

标签: ruby-on-rails heroku


【解决方案1】:

我认为您的配置看起来适合主机。


解决方案

为生产环境启用ActionMailer设置:

ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  address:        'smtp.sendgrid.net',
  port:           '587',
  authentication: :plain,
  user_name:      ENV['SENDGRID_USERNAME'],
  password:       ENV['SENDGRID_PASSWORD'],
  domain:         'heroku.com',
  enable_starttls_auto: true
}

可以在config/environments/production.rb 或初始化脚本中指定这些设置。

如果使用初始化脚本,请确保任何依赖于环境的 if 块保护包括生产,例如,config/initializers/setup_mail.rb

if Rails.env.development? || Rails.env.production?
    ... # ActionMailer config goes here.
end

原始答案建议

现在只有三种可能性浮现在脑海中,这可能是问题所在。这些可能不正确,但在这里。

错误信息来自哪里?通过运行heroku logs?从本地环境的终端?如果是您的本地环境,请检查您的 config/environments/development.rb 文件(可能还有 test.rb)是否配置了主机选项。

也许更改没有进入生产环境,或者生产环境没有在 heroku 上运行。

您可以检查heroku config 没有设置RAILS_ENV 变量,或者如果有,请确保将其设置为production。 (在您的应用目录中运行 heroku config)。

检查最新代码是否已提交并推送到 Heroku:

git status
git add . # Only if there are unstaged changes
git commit -m "Message describing changes"
git push heroku master

如果没有需要推送到 Heroku 的更改,请尝试 heroku restart

【讨论】:

  • 我正在使用 Devise 发送确认说明电子邮件,这意味着它在请求期间,对吗?我通过在终端中运行 heroku 日志找到了错误消息。按照您的建议,我运行了 heroku 配置,并且我的 RAILS_ENV 设置为生产。我最新的代码已经提交,我终于尝试了heroku重启。不过,我仍然遇到同样的错误。还有其他想法吗?
  • 查看 heroku 日志错误,并阅读它的前几行。是否有一个模板文件,其中指定了行号?如果是,那就是问题的根源。您可以通过评论/删除该行来确认它。您可能还想在问题中包含整个错误跟踪。
  • 我现在将在问题中添加整个错误跟踪。它指的是confirmation_instructions 文件中的第5 行。我不明白这怎么可能是问题所在,因为 Devise 为我生成了所有内容并且我没有更改代码。
  • 我的想法已经不多了。是否在应用程序的其他任何地方设置了 default_url_options?也许 production.rb 中的设置正在其他地方被清除。在代码库中搜索default_url_options
  • 还有一个想法——你是在一个 git 分支上,并且已经将更改合并到 master 中了吗?
【解决方案2】:

尝试将:host 选项传递给confirmation_url(@resource, confirmation_token: @token),例如

confirmation_url(@resource, confirmation_token: @token, host: 'myapp.herokuapp.com')

(虽然在模板中传递硬编码的生产 URL 感觉不正确,但它可能会让您更接近解决问题)

【讨论】:

  • 艾略特,我是否将该代码添加到 production.rb 文件中的其他代码中? (我是新手!)谢谢!
  • 这是对您添加到问题中的确认说明视图模板的修改。
  • 再次感谢,现在我尝试使用上面的代码,现在在终端的 heroku 日志中显示以下错误: Completed 500 Internal Server Error in 123ms, Errno::ECONNREFUSED (Connection denied - connect( 2)):以及一长串供应商文件名。这意味着什么?
  • 下面有更详细的错误吗?如果没有,请尝试另一个请求。也可以在您的开发环境中尝试它,以确保至少它首先在那里工作。
  • 不,没有其他错误参考。只是一个很长的供应商文件名列表。我已经试了三遍了。每次回来都是一样的。我也刚刚在开发中再次尝试过,它在那里完美运行。
猜你喜欢
  • 2014-04-12
  • 1970-01-01
  • 2017-12-31
  • 2019-12-11
  • 2011-05-06
  • 1970-01-01
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多