【问题标题】:Crash in Rails ActionView related to I18n.fallbacks when invoked from delayed_job从 delay_job 调用时,与 I18n.fallbacks 相关的 Rails ActionView 崩溃
【发布时间】:2020-12-18 10:30:45
【问题描述】:

我正在尝试从我的 rails 4 应用程序发送一封电子邮件(来自控制台的精简版):

> ActionMailer::Base.mail(from: 'mail@example.com', to: 'foo@example.com', subject: 'test', body: "Hello, you've got mail!").deliver_later

邮件将由delayed_lob 发送,在我的本地测试设置中我会这样触发它:

> Delayed::Job.last.invoke_job

但是,作业崩溃并显示以下消息:

Devise::Mailer#invitation_instructions: processed outbound mail in 56234.1ms
Performed ActionMailer::DeliveryJob from DelayedJob(mailers) in 56880.04ms
TypeError: no implicit conversion of nil into Array
    from /Users/de/.rvm/gems/ruby-2.4.0/gems/actionview-4.2.10/lib/action_view/lookup_context.rb:51:in `concat'
    from /Users/de/.rvm/gems/ruby-2.4.0/gems/actionview-4.2.10/lib/action_view/lookup_context.rb:51:in `block in <class:LookupContext>'
    from /Users/de/.rvm/gems/ruby-2.4.0/gems/actionview-4.2.10/lib/action_view/lookup_context.rb:39:in `initialize_details'
    from /Users/de/.rvm/gems/ruby-2.4.0/gems/actionview-4.2.10/lib/action_view/lookup_context.rb:205:in `initialize'
...

我查看了lookup_context.rb:51GitHub的代码,问题出在这里:

register_detail(:locale) do
      locales = [I18n.locale]
      locales.concat(I18n.fallbacks[I18n.locale]) if I18n.respond_to? :fallbacks  # < crashes here
# from the debugger I got:
# I18n.locale => :de
# I18n.fallbacks => {:en=>[]}

所以显然后备不包含我的语言环境 (:de),这会导致 nil 异常。

显然 I18n.fallbacks 配置不正确。

问题:我该如何解决这个问题?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 actionmailer delayed-job actionview


    【解决方案1】:

    您需要配置您的默认和后备语言环境,如下所示

    config.i18n.default_locale = :de
    config.i18n.fallbacks = { de: :en }
    

    请试试这个。如果找不到:de,它将回退到:en

    【讨论】:

      【解决方案2】:

      在我找到的这篇博文的帮助下我得到了答案:https://sjoker.net/2013/12/30/delayed_job-and-localization/
      它包含了我需要的一半。建议的解决方案如下:

      为了从创建作业时传播状态,该状态必须通过将其存储在数据库中的作业对象中来转移到调用作业的时间。
      在博文中,作者仅存储当前语言环境以在邀请时对邮件进行本地化。但是,我还需要存储 fallbacks,这需要一点序列化。
      这是我的解决方案:

      # Add a state attributes to delayed_jobs Table
      class AddLocaleToDelayedJobs < ActiveRecord::Migration
        def change
          change_table :delayed_jobs do |t|
            t.string :locale    # will hold the current locale when the job is invoked
            t.string :fallbacks # ...
          end
        end
      end
      
      # store the state when creating the job
      Delayed::Worker.lifecycle.before(:enqueue) do |job|
        # If Locale is not set
        if(job.locale.nil? || job.locale.empty? && I18n.locale.to_s != I18n.default_locale.to_s)
          job.locale    = I18n.locale
          job.fallbacks = I18n.fallbacks.to_json
        end
      end
      
      # retrieve the state when invoking the job
      Delayed::Worker.lifecycle.around(:invoke_job) do |job, &block|
        # Store locale of worker
        savedLocale        = I18n.locale
        savedFallbacks     = I18n.fallbacks
      
        begin
          # Set locale from job or if not set use the default
          if(job.locale.nil?)
            I18n.locale    = I18n.default_locale
          else
            h              = JSON.parse(job.fallbacks, {:symbolize_names => true})
            I18n.fallbacks = h.each { |k, v| h[k] = v.map(&:to_sym) } # any idea how parse this more elegantly?
            I18n.locale    = job.locale
          end
      
          # now really perform the job
          block.call(job)
      
        ensure
          # Clean state from before setting locale
          I18n.locale      = savedLocale
          I18n.fallbacks   = savedFallbacks
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-18
        • 1970-01-01
        相关资源
        最近更新 更多