0

I have a rake task which is run on deploy to generate the service_worker.js file. There's a ServiceWorkerController for that purose:

app/controllers/service_worker_controller.rb

class ServiceWorkerController < ActionController::Base
  def service_worker
  end
end

app/views/service_worker.js.erb (simplified)

'use strict';

const offlinePages = [
  '<%= root_path %>'
]

lib/tasks/statics.rake (simplified)

namespace :statics do

  desc "Generate static files."
  task :create => :environment do
    I18n.locale = :en
    File.open(Rails.root.join('service_worker.js'), 'w') do |f|
      f.write ServiceWorkerController.render(:service_worker)
    end
  end

end

Running rails statics:create works fine up to the latest Rails 6.0, however, after upgrading to the latest Rails 6.1, the root_path in the view causes the following exception:

ActionView::Template::Error: wrong number of arguments (given 4, expected 0..1) /home/rails/app/views/service_worker/service_worker.js.erb:4:in `_app_views_service_worker_service_worker_js_erb__1965251932277058399_91060'

Any ideas which change from Rails 6.0 to 6.1 might explain this?

Update Forgot to mention that ServiceWorkerController.render(:service_worker) works just fine in the Rails console, apparently, there's something missing in the context of a rake task despite task :create => :environment.

svoop
  • 3,318
  • 1
  • 23
  • 41
  • It seems that the error is raised in the service_worker.js.erb file so you'd need to share its contents or at least first 15 lines. – Kamil Gwóźdź Feb 14 '22 at 21:40
  • Never mind the `:15` in the error message, the error is triggered by `root_path`. Only in the unsimplified version of the file (which the error message is for) has some more lines of irrelevant static yaddayadda on the top. I'll redact the error message accordingly. – svoop Feb 16 '22 at 08:17

0 Answers0