15. class BlogController < ApplicationController
def list
unless read_fragment(:action => 'list' )
@articles = Article.find_recent
end
end
end
<% cache do %> <!- Here's the content we cache ->
<ul>
<% for article in @articles -%>
<li><p><%= h(article.body) %></p></li>
<% end -%>
</ul>
<% end %>
16. 某些时候因为@articles没有初始化而出错
详见Robin Lu于RubyConf China 2009中的演讲
“Ruby on Rails Pitfalls”,Page 10
17. public class BlogController : Controller {
public ActionResult List() {
var model = LazyBuilder.Create<Model>()
.Setup(m => m.Articles, () => Article.FindRecent())
.Instance;
return View(model);
}
}
<% Html.Cache("recent-articles", () => { %>
<ul>
<% foreach (var article in Model.Articles) { %>
<li><p><%= article.Body %></p></li>
<% } %>
</ul>
<% } %>