使用JBuilder和Rails 6进行片段缓存时,未定义方法'perform_caching'

问题描述 投票:1回答:1

我正在尝试将片段缓存应用于我的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

我在Gemfile中添加了以下宝石:

gem "actionpack-page_caching", "~> 1.2"
gem "actionpack-action_caching", "~> 1.2"

在我的application.rb中,添加了以下几行:

config.action_controller.perform_caching = true
config.cache_store = :memory_store, { size: 64.megabytes }

要在jbuilder视图中启用片段缓存,还需要什么?

我正在使用jbuilder 2.7的Rails 6.0.2。

ruby-on-rails ruby-on-rails-6 jbuilder
1个回答
0
投票

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

class ApplicationController < ActionController::API                                                    
  include ActionController::Caching
  # ...
end

或者如果仅希望在此控制器中使用缓存,则可能仅将其包括在Api::CountriesController中>

请参见https://github.com/rails/jbuilder/issues/331

我也相信您不需要其他宝石

gem "actionpack-page_caching", "~> 1.2"
gem "actionpack-action_caching", "~> 1.2"
© www.soinside.com 2019 - 2024. All rights reserved.