【发布时间】:2012-09-15 13:02:09
【问题描述】:
到目前为止,缓存是我的视图代码中逻辑最密集的部分,所以我想从装饰器内部进行片段缓存,但是我做不到。
当我通过我的装饰器执行此操作时:
def cached_name
h.cache do
"a name here"
end
end
我明白了:
当你没想到时,你有一个 nil 对象!你可能有 期望一个 Array 的实例。评估时发生错误 无长度
我从控制器内部实例化我的装饰器
@presenter = SomePresenter::new
我将 HAML 用于我的视图
我怎样才能成功地从我的装饰器中缓存,所以我的视图可以做这样的事情
= @decorator.cached_logic_heavy_stuff
更新:我创建了一个 git 存储库来显示我的问题:https://github.com/houen/presenter_caching
更新:这可能有效 - 请参阅 repo
include Haml::Helpers
def another_way_to_try
self.init_haml_helpers
buffer = haml_buffer.buffer
h.with_output_buffer(buffer) do
h.cache do
h.concat "i should still not be empty"
end
end
end
【问题讨论】:
-
你在这里缓存什么?我没有看到任何块作为参数
标签: ruby-on-rails caching haml draper