我正在尝试将片段缓存应用于我的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。
我相信您需要将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"