简体   繁体   English

耙任务和action_mailer.default_url_options

[英]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] , or set :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.rbtest.rbproduction.rb (如果你有他们更多)与相应的主机名各一个

Based Link 基础链接

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.

相关问题 为什么不能通过Devise 3和Rails 4为action_mailer.default_url_options设置主机名? - Why can't I set host name for action_mailer.default_url_options by Devise 3 and Rails 4? Rails / Devise / Mailer:尝试注册时抛出“ config.action_mailer.default_url_options”错误 - Rails / Devise / Mailer: Throwing 'config.action_mailer.default_url_options' error on attempted registration 默认网址在动作邮件中无法正常工作 - default url in action mailer not working 如何使用domain.name设置'config.action_mailer.default_url_options'的值? - How can I set the value of 'config.action_mailer.default_url_options' with domain.name? 在rails中将config.action_mailer.default_url_options设置为该应用的当前主机是什么? - in rails set config.action_mailer.default_url_options to whatever the current host of the app is? Rails Mailer和Rake任务的问题 - Issue with rails mailer and rake task rails config mailer 默认 url 选项未生效(开发环境) - rails config mailer default url options not taking effect (development environment) 如何设置每个邮件的default_url_options? - How can I set default_url_options per mailer? 如何从Rails 2.2.2中的rake任务调用模型/动作邮件程序方法 - How to call model /action mailer methods from a rake task in rails 2.2.2 Rails default_url_options用于耙,延迟作业等 - Rails default_url_options for rake, delayed jobs etc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM