Hi,
I’m new to fragment caching, and was wondering the best way to do it for
a dynamically generated page. I have been working off of the “Agile Web
Development with Rails” book, but it’s rather brief on fragment caching.
For example, suppose you have a list of manufacturers and each of which
has many products. Here is how I would implement the caching, but I’m
not clear if it will work, or if there’s a cleaner, less clunky of doing
it:
Product Controller:
def list
@mid = params[:manufacturer_id]
if @mid.nil?
@mid = “all”
unless read_fragment(:action => ‘list’, :mid => “all”)
@products = Product.find(:all)
end
else
unless read_fragment(;action => ‘list’, :mid => @mid.to_s)
@products = Product.find_by_manufacturer_id(@mid)
end
end
end
View (list.rhtml)
<% cache(:action =>‘list’, :mid => @mid) do %>
-
<% for p in @products %>
- <%= p.name %> <% end %>
Any suggestions or advice on a better way to to go about doing this
would be much appreciated. Thanks.