undefined method `html_fallback_for_js' in ActionView::Base

114 views Asked by At

I have recently upgraded a project to ruby 3.2.2 and rails 6.1 I am having issues with ActionView::Base. With the recent upgrade, I need to pass 3 arguments to ActionView::Base.new instead of 2. From the docs, ActionView now receives lookup_context, assigns, controller

view = ActionView::Base.new(
  ActionController::Base.view_paths, 
 {
  client: client,  
  report: self.report, 
},
nil
)

So I fixed it by passing nil (as shown above), but now I get a different error:

/usr/local/rvm/gems/ruby-3.2.2/gems/actionview-6.1.7.3/lib/action_view/base.rb:264:in `in_rendering_context': undefined method `html_fallback_for_js' for #<ActionView::PathSet:0x0000000110898f58 @paths=[#<ActionView::OptimizedFileSystemResolver:0x000000010fa92670 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x0000000110898f30 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x0000000110898eb8>, @cache=#<ActionView::Resolver::Cache:0x0000000110898e90 keys=0 queries=0>, @path="/Users/Mari/reporting/app/views">, #<ActionView::OptimizedFileSystemResolver:0x000000010fa92760 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x00000001108994a8 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x0000000110899408>, @cache=#<ActionView::Resolver::Cache:0x00000001108993e0 keys=0 queries=0>, @path="/usr/local/rvm/gems/ruby-3.2.2/gems/graphiql-rails-1.9.0/app/views">, #<ActionView::OptimizedFileSystemResolver:0x000000010fa92850 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x0000000110899ac0 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x0000000110899a48>, @cache=#<ActionView::Resolver::Cache:0x0000000110899a20 keys=0 queries=0>, @path="/usr/local/rvm/gems/ruby-3.2.2/gems/teaspoon-1.2.2/app/views">, #<ActionView::OptimizedFileSystemResolver:0x000000010fa92940 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x0000000110899e80 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x0000000110899e08>, @cache=#<ActionView::Resolver::Cache:0x0000000110899de0 keys=0 queries=0>, @path="/usr/local/rvm/gems/ruby-3.2.2/gems/devise-security-0.18.0/app/views">, #<ActionView::OptimizedFileSystemResolver:0x000000010fa92990 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x000000011089a268 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x000000011089a1f0>, @cache=#<ActionView::Resolver::Cache:0x000000011089a1c8 keys=0 queries=0>, @path="/usr/local/rvm/gems/ruby-3.2.2/gems/devise-4.8.0/app/views">, #<ActionView::OptimizedFileSystemResolver:0x000000010fa929e0 @pattern=":prefix/:action{.:locale,}{.:formats,}{+:variants,}{.:handlers,}", @unbound_templates=#<Concurrent::Map:0x000000011089a740 entries=0 default_proc=nil>, @path_parser=#<ActionView::Resolver::PathParser:0x000000011089a6c8>, @cache=#<ActionView::Resolver::Cache:0x000000011089a650 keys=0 queries=0>, @path="/usr/local/rvm/gems/ruby-3.2.2/gems/ckeditor-4.3.0/app/views">]> (NoMethodError)

      if !lookup_context.html_fallback_for_js && options[:formats]

The actionview version is 6.1.7.3

2

There are 2 answers

1
mechnicov On

In Rails 6.1 ActionView::Base.new takes 3 arguments: lookup_context, assigns, controller

But you passed ActionView::PathSet instance instead of ActionView::LookupContext

This object hasn't such getter, that's why you have error

Instead of this wrap path set into lookup context. Also in new Ruby don't need duplicate value if it's name is equal of key name

view =
  ActionView::Base.new(
    ActionView::LookupContext.new(ActionController::Base.view_paths),
    { client:, report: },
    nil,
  )

After that you can use this view for rendering

0
marimaf On

From this answer

I have changed the call from ActionView::Base.new to ActionView::Base.with_empty_template_cache.new

and also one of the arguments from context to a LookupContext like this: from ActionController::Base.view_paths to ActionView::LookupContext.new(ActionController::Base.view_paths)

the call now looks like this:

   view = ActionView::Base.with_empty_template_cache.new(
      ActionView::LookupContext.new(ActionController::Base.view_paths), 
     {
      client: client, 
      report: self.report, 
    }, nil
    )