svoop
svoop

Reputation: 3454

ArgumentError when rending view in rake task

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.

Upvotes: 0

Views: 110

Answers (0)

Related Questions