Ruby on rails 未定义的方法';执行#u缓存';使用JBuilder和Rails 6进行片段缓存时

Ruby on rails 未定义的方法';执行#u缓存';使用JBuilder和Rails 6进行片段缓存时,ruby-on-rails,ruby-on-rails-6,jbuilder,Ruby On Rails,Ruby On Rails 6,Jbuilder,我正在尝试将片段缓存应用于我的一个jbuilder视图: # frozen_string_literal: true json.cache!(['1', @countries], expires_in: 60.minutes) do json.partial!('country', collection: @countries, as: :country) end 不幸的是,这会输出以下错误: ActionView::Template::Error (undefined method `

我正在尝试将片段缓存应用于我的一个jbuilder视图:

# frozen_string_literal: true

json.cache!(['1', @countries], expires_in: 60.minutes) do
  json.partial!('country', collection: @countries, as: :country)
end
不幸的是,这会输出以下错误:

ActionView::Template::Error (undefined method `perform_caching' for #<Api::CountriesController:0x00005572a6187f90>):
  1: # frozen_string_literal: true
  2: 
  3: json.cache!(['1', @countries], expires_in: 60.minutes) do
  4:   json.partial!('country', collection: @countries, as: :country)
  5: end
在我的
application.rb
中,我添加了以下几行:

config.action_controller.perform_caching = true
config.cache_store = :memory_store, { size: 64.megabytes }
在jbuilder视图中启用片段缓存还需要什么


我使用的是jbuilder 2.7的Rails 6.0.2。

我认为您需要将
ActionController::Caching
包含到基本控制器
ApplicationController
中,因为它未包含在
ActionController::API

class ApplicationController < ActionController::API                                                    
  include ActionController::Caching
  # ...
end
class ApplicationController < ActionController::API                                                    
  include ActionController::Caching
  # ...
end
gem "actionpack-page_caching", "~> 1.2"
gem "actionpack-action_caching", "~> 1.2"