【问题标题】:rails config mailer default url options not taking effect (development environment)rails config mailer 默认url选项不生效(开发环境)
【发布时间】:2019-04-06 08:41:15
【问题描述】:

我正在调整使用创建的邮件中显示的 url,

<%= controller_url %>

但是当我使用以下命令更改默认值时

config.action_mailer.default_url_options = { :host => "example.com", :protocol => 'https', :subdomain => "www"}

网址保持不变

http://localhost:8000/controller

这不起作用的一些可能原因是什么?我非常彻底地查看了我的项目文件夹中可能定义了主机或子域的地方,但什么也没找到。

编辑: 进行更改后,我似乎需要重新启动服务器。

【问题讨论】:

  • 我相信默认 url 正在生产中实现,这是需要更改的原因。 (拆分 :host 和 :subdomain)

标签: ruby-on-rails linux server localhost host


【解决方案1】:

我不知道您是如何尝试的,但我可以展示我的文件以与您的文件进行比较。

所以我的开发文件是/config/environments/development.rb

config.consider_all_requests_local = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

我的生产文件是/config/environments/production.rb

config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { :host => "https://example.com" } #=> Or https://www.example.com
config.action_mailer.perform_deliveries = true

ActionMailer::Base.smtp_settings = {
    :user_name => ENV['USERNAME'],
    :password => ENV['PASSWORD'],
    :domain => 'example.com',
    :address => 'smtp.example.net',
    :port => 321,
    :authentication => :plain,
    :enable_starttls_auto => true
}
config.action_mailer.delivery_method = :smtp

它运行良好。

记住这些:

  • 仅当项目处于开发模式时,开发文件才有效
  • 仅当项目处于生产模式时,生产文件才有效
  • 并确保在更改与配置相关的任何内容后重新启动服务器

您可以在发送邮件时检查日志发生了什么,生产运行请按照此命令rails server -e production

【讨论】:

  • 谢谢。如果您在开发中更改主机:'localhost' 部分,它会更改您生成的网址吗?通过阅读 rails 指南,我认为它应该,但对我来说并没有改变。获得此信息有助于确定项目中是否有其他人所做的设置覆盖了此设置,或者这是否是正常行为。
  • @SamuelFrost 是的!它正在改变我确定我刚刚再次检查并且我的日志显示&lt;a href="http://example:3000/login"&gt;Click here&lt;/a&gt; to login,这里我只是更改主机example 并重新启动服务器。确保在更改配置后重新启动服务器
  • 谢谢,这有助于缩小范围!但我仍然对为什么它不起作用感到困惑。我只能想象有一个 gem 在幕后做某事,或者在操作系统级别发生了什么。
  • 啊,是重启服务器!当我读到开发配置文件在每次请求时都会重新加载时,我以为它正在更新。
  • 很高兴听到它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-14
  • 2014-07-25
  • 2018-07-11
  • 2020-07-07
  • 2015-06-16
  • 2022-07-13
  • 1970-01-01
相关资源
最近更新 更多