【问题标题】:Missing host to link to action mailer缺少主机链接到行动邮件
【发布时间】:2013-11-30 15:55:50
【问题描述】:

我正在使用 Rails 和设计,我正在尝试发送电子邮件,但在生产和测试环境中出现以下错误,在开发中一切正常:

ActionView::Template::错误: 缺少要链接的主机!请提供 :host 参数,设置 default_url_options[:host],或设置 :only_path 为 true # ./app/views/devise/mailer/reset_password_instructions.html.erb:9:in `_app_views_devise_mailer_reset_password_instructions_html_erb___200197999_97517150'

我的发展.rb

config.action_mailer.delivery_method = :smtp
  config.action_mailer.default_url_options = { :host => "localhost:3000" }
  config.action_mailer.perform_deliveries = true
  config.action_mailer.raise_delivery_errors = true
  config.action_mailer.default :charset => "utf-8"

test.rb

config.action_mailer.delivery_method = :test
config.action_mailer.default_url_options = { :host => "localhost:3000" }

生产.rb

  config.action_mailer.default_url_options = { :host => "mydomain.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"

reset_password_instructions.html.erb

<p>Hello, <%= @resource.full_name %>!</p>

<p>You have request a new passord</p>

    <p><%= link_to 'Change my password', edit_password_url(@resource, :reset_password_token => @token), class: 'btn btn-info' %></p>
    <p>If you didn't request this, please ignore this email.</p>
    <p>Your password won't change until you access the link above and create a new one.</p>

哪里出错了?

【问题讨论】:

    标签: ruby-on-rails devise actionmailer


    【解决方案1】:

    我不确定这是否是拼写错误,但看起来测试和生产的主机定义设置不正确。

    test.rb:

    config.action_mailer.default_url_options = { :host => "http://loclahost:3000" }
    

    生产.rb:

    config.action_mailer.default_url_options = { :host => "http:/mydomain.com" }
    

    一旦设置正确,它应该可以工作。例如:

    test.rb:

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

    生产.rb:

    config.action_mailer.default_url_options = { :host => "mydomain.com" }
    

    【讨论】:

    • 嗨,我用你的换了行,但仍然有同样的错误,好像有一个变量找不到,(我将编辑我的问题并更正网址)
    • 服务器重启了吗?另外,您能否描述一下应用在生产环境中的部署位置?
    • 是的,我重新启动服务器,应用程序部署在heroku上
    • 我找到了一个 intializer/setup_mailer.rb,其中包含以下行: ActionMailer::Base.default_url_options[:host] = ENV["DEFAULT_HOST"] 实际上可能会覆盖 production.rb 中的代码env 变量 DEFAULT_HOST 未在 heroku 上设置,在生产中设置后一切正常。顺便说一句,在测试环境中仍然有同样的问题。
    【解决方案2】:

    最后我在每个环境文件中添加了正确的值:

    test.rb / development.rb / production.rb

    设计 3.2.x

    Rails 4.1.x

    感谢maudulus

    # Default Mailer Host
      Rails.application.routes.default_url_options[:host] = 'domain.com'
    

    *注意不要从 ENV 变量中加载值,否则会抛出错误

    【讨论】:

    • 感谢您添加此注释。我正在从 Figaro 加载值,但不知道为什么它不起作用!
    【解决方案3】:

    我已经通过这种方式解决了问题:https://stackoverflow.com/a/9402816/1759437,在 application_controller 上设置 default_url_options。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-30
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多