I have some code like this:
我有一些像这样的代码:
<% cache "footer_links" do %>
<%= cms_snippet_content('footer_links') %>
<% end %>
And I thought of writing a helper method, like this one:
我想写一个帮助方法,就像这样:
def cached_snippet_content(snip_id)
cache(snip_id) do
cms_snippet_content(snip_id)
end
end
However, I don't get any output in my view, even though, my erb code looks like this:
但是,我的视图中没有得到任何输出,即使我的erb代码如下所示:
<%= cached_snippet_content "footer_links" %>
What am I doing wrong?
我究竟做错了什么?
3 个解决方案
#1
1
May the source be with you, Luke:
可能是你的来源,卢克:
# actionpack-3.2.0/lib/action_view/helpers/cache_helper.rb
def cache(name = {}, options = nil, &block)
if controller.perform_caching
safe_concat(fragment_for(name, options, &block))
else
yield
end
nil
end
This shows that cache
is implemented to be called from ERB views, not from helpers. An alternative implementation:
这表明缓存实现为从ERB视图调用,而不是从助手调用。另一种实现方式:
def cache(name = {}, options = nil, &block)
if controller.perform_caching
fragment_for(name, options, &block)
else
capture(&block)
end
end
And now use it with the new Rails ERB style (<%= ... > even in blocks if they output something):
现在将它与新的Rails ERB样式一起使用(<%= ...>即使在块中输出内容也是如此):
<%= cache "key" do %>
<%= content_tag(:p, "hello") %>
<% end %>
I'd test this carefully, there may be hidden corners, I guess there'll be a reason why cache
has not been adapted to the Rails 3 block style.
我会仔细测试这个,可能有隐藏的角落,我想有一个原因,为什么缓存没有适应Rails 3块样式。
#2
0
It looks like the do
block in your helper method isn't returning anything and hence the entire helper method isn't returning anything and henceforth there's nothing for the view to display.
看起来你的helper方法中的do块没有返回任何东西,因此整个helper方法没有返回任何东西,从此以后没有任何东西可以显示视图。
Maybe try this:
也许试试这个:
def cached_snippet_content(snip_id)
cache(snip_id) do
result = cms_snippet_content(snip_id)
end
result
end
#3
0
Try this:
尝试这个:
def cached_snippet_content(snip_id)
a = ""
cache(snip_id) do
a += cms_snippet_content(snip_id).to_s
end
a
end
#1
1
May the source be with you, Luke:
可能是你的来源,卢克:
# actionpack-3.2.0/lib/action_view/helpers/cache_helper.rb
def cache(name = {}, options = nil, &block)
if controller.perform_caching
safe_concat(fragment_for(name, options, &block))
else
yield
end
nil
end
This shows that cache
is implemented to be called from ERB views, not from helpers. An alternative implementation:
这表明缓存实现为从ERB视图调用,而不是从助手调用。另一种实现方式:
def cache(name = {}, options = nil, &block)
if controller.perform_caching
fragment_for(name, options, &block)
else
capture(&block)
end
end
And now use it with the new Rails ERB style (<%= ... > even in blocks if they output something):
现在将它与新的Rails ERB样式一起使用(<%= ...>即使在块中输出内容也是如此):
<%= cache "key" do %>
<%= content_tag(:p, "hello") %>
<% end %>
I'd test this carefully, there may be hidden corners, I guess there'll be a reason why cache
has not been adapted to the Rails 3 block style.
我会仔细测试这个,可能有隐藏的角落,我想有一个原因,为什么缓存没有适应Rails 3块样式。
#2
0
It looks like the do
block in your helper method isn't returning anything and hence the entire helper method isn't returning anything and henceforth there's nothing for the view to display.
看起来你的helper方法中的do块没有返回任何东西,因此整个helper方法没有返回任何东西,从此以后没有任何东西可以显示视图。
Maybe try this:
也许试试这个:
def cached_snippet_content(snip_id)
cache(snip_id) do
result = cms_snippet_content(snip_id)
end
result
end
#3
0
Try this:
尝试这个:
def cached_snippet_content(snip_id)
a = ""
cache(snip_id) do
a += cms_snippet_content(snip_id).to_s
end
a
end