[英]rake task and action_mailer.default_url_options
When I try to use ActionMailer from rake task I'm getting this error: 当我尝试通过rake任务使用ActionMailer时,出现此错误:
ActionView::Template::Error: Missing host to link to!
ActionView :: Template :: Error:缺少要链接的主机! Please provide the :host parameter,
set default_url_options[:host]
, orset :only_path
to true请提供:host参数,
set default_url_options[:host]
或set :only_path
为true
In my development.rb
I have the following line: 在我的
development.rb
,有以下一行:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Moreover, if I put in development.rb
this: 此外,如果我输入
development.rb
,则:
config.action_mailer.default_url_options[:host] = 'localhost:3000'
rake task works fine, but server crashes with this: rake任务运行正常,但是服务器崩溃:
config/environments/development.rb:20:in block in ': undefined method []=' for nil:NilClass (NoMethodError)
config / environments / development.rb:20:在nil:NilClass的':undefined method [] ='中的块(NoMethodError)
If I do this: 如果我这样做:
config.action_mailer.default_url_options = {}
config.action_mailer.default_url_options[:host] = 'localhost:3000
rake task doesn't work again. 耙任务不再起作用。
I'm confused, any suggestions? 我很困惑,有什么建议吗?
UPD i don't think that code will help, but here it is, it's that simple: UPD我认为代码不会有所帮助,但是就在这里,就这么简单:
rake task: 耙任务:
task :send_newsletter => :environment do
Receiver.all.each { |receiver|
NewsletterMailer.newsletter(receiver).deliver
}
end
mailer: 邮件:
def newsletter(receiver)
@receiver = receiver
mail(to: receiver.email, subject: "newsletter") do |format|
format.html
end
end
and the view with some link_to xxx_url
lines, apparently which causing the error 和带有一些
link_to xxx_url
行的视图,显然这会导致错误
ruby version 2.4.1 红宝石版本2.4.1
rails 5.1.3 导轨5.1.3
rake 12.0.0 (but also tried 12.3.0 with no difference) 耙12.0.0(但也尝试12.3.0没什么区别)
Try the following, that should work 尝试以下方法,应该可以
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Update 更新资料
I don't know what is the ideal solution for this? 我不知道什么是理想的解决方案? but I have tested and it working for me
但是我已经测试过了,对我有用
Rails.application.routes.default_url_options = config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
This also work 这也可以
Rails.application.routes.default_url_options = Rails.application.config.action_mailer.default_url_options
This solution based on this link 此解决方案基于此链接
And another one is 还有一个是
Rails.application.routes.default_url_options[:host] = '???'
This goes in each environment file - development.rb
, test.rb
and production.rb
(and more if you have them) with the corresponding hostname in each one 这正好在每个环境文件-
development.rb
, test.rb
和production.rb
(如果你有他们更多)与相应的主机名各一个
Hope it will help 希望对你有帮助
solved with this line added to rake task. 解决此行添加到耙任务。 based on fool-dev's answer
基于傻瓜开发的答案
Rails.application.routes.default_url_options = Rails.application.config.action_mailer.default_url_options
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.