Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails部分缓存显示视图更改,尽管没有模型更改_Ruby On Rails_Ruby On Rails 4_Caching - Fatal编程技术网

Ruby on rails Rails部分缓存显示视图更改,尽管没有模型更改

Ruby on rails Rails部分缓存显示视图更改,尽管没有模型更改,ruby-on-rails,ruby-on-rails-4,caching,Ruby On Rails,Ruby On Rails 4,Caching,遵循“使用Rails 4进行敏捷Web开发”的指南 它涉及产品目录的缓存,以仅重新呈现已更改的产品 我编辑了(config/environments/development.rb): 添加了返回最新更新产品的代码: (app/models/product.rb) 最后更新了缓存的存储索引: (app/views/store/index.html.erb) 你的实用目录 这本书指出,测试缓存是否工作的唯一方法是更改视图,因此我所做的只是在一个或两个缓存标记中添加一些lorem ipsum,但浏览

遵循“使用Rails 4进行敏捷Web开发”的指南 它涉及产品目录的缓存,以仅重新呈现已更改的产品

我编辑了(config/environments/development.rb):

添加了返回最新更新产品的代码: (app/models/product.rb)

最后更新了缓存的存储索引: (app/views/store/index.html.erb)

你的实用目录
这本书指出,测试缓存是否工作的唯一方法是更改视图,因此我所做的只是在一个或两个缓存标记中添加一些lorem ipsum,但浏览器在刷新时立即显示更改。。。令人沮丧的!有什么线索吗?

尝试使用

<% cache ['v1', Product.order(updated_at: :desc).last] do %>

而不是

<% cache ['store', Product.latest] do %>

集合的缓存密钥未更改。这是因为
cache['store',Product.latest]
并不像您预期的那么聪明。如果查看日志,您将看到所使用的缓存键是一个文本,其中包含类似ActiveRecord::Relation::Products的内容

使用上次更新的\u at值
缓存['store',Product.latest.max(:updated\u at)]
以获得更好的结果


在Rails 5中,由于几周前的原因,这将变得更容易。

您在
产品上遇到了一个bug。最新的
,它正在调用自己。您可能有一个
Product.order(:updated_at)。最后(10)
如果没有出现错误,可能是明显的,但您在更改环境文件后重新启动了服务器吗?
<h1>Your Pragmatic Catalog</h1>
<% cache ['store', Product.latest] do %>
  <% @products.each do |product| %>
    <% cache ['entry', product] do %>
      <div class="entry">
        <%= image_tag(product.image_url) %>
        <h3><%= product.title %></h3>
        <%= sanitize(product.description) %>
        <div class="price_line">
          <span class="price"><%= number_to_currency(product.price) %></span>
        </div>
      </div>
    <% end %>
  <% end %>
<% end %>
<% cache ['v1', Product.order(updated_at: :desc).last] do %>
<% cache ['store', Product.latest] do %>