注意:我在这里提出了一个逻辑,我在做什么。format.js在操作缓存启用时不会操纵dom
我在做什么:
想想基本指标的行动,我们的上市产品,并与分页。现在使用remote-true选项,我启用了基于Ajax的分页。到目前为止,事情很好。看看示例代码。
产品控制器:
def index
@products = Product.paginate(:order =>"name ASC" ,:page => params[:page], :per_page => 14)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @products }
format.js
end
end
Index.html.erb
<h1>Products</h1>
<div id="products">
<%= render "products/products" %> // products partial is just basic html rendering
</div>
<script>
$(function(){
$('.pagination a').attr('data-remote', 'true')
});
</script>
index.js.erb的
jQuery('#products').html("<%= escape_javascript (render :partial => 'products/products') %>");
$('.pagination a').attr('data-remote', 'true');
所以什么问题:
现在我想对此启用操作缓存。但是index.js.erb文件不处理DOM。如果我删除远程真正的功能,那么事情与缓存工作正常。
对于动作缓存我已经加入这一行的控制器的顶部:
caches_action :index, :cache_path => Proc.new { |c| c.params }
有什么建议?
更新:
问题是jQuery代码不执行。从this问题
我发现了什么问题。 jQuery实际上将传入的脚本用a包围,以便浏览器评估传入的代码。但缓存机制仅仅将代码保存为文本,并且当重新请求时,它将代码作为文本返回,但不对其进行评估。因此,需要明确评估代码
但是如何解决这个问题?
尝试'caches_action:index,:cache_path => Proc.new {| c | c.params.merge(格式:request.format。to_s)}' – jvnill
你能检查caches_action是否也缓存你渲染的部分?打开浏览器控制台并检查您收到的回复。在这种情况下,你的问题不是它不更新DOM,而是用相同的部分更新它,因此结果相同。 – mathieugagne
所以...你检查了吗? – mathieugagne