fresh_when在ruby中不使用xml渲染

时间:2009-11-30 16:46:22

标签: ruby caching etag

在尝试在休息系统中实现对条件GET的支持时,我们遇到了fresh_when和陈旧?方法

以下代码适用于304而不进一步呈现:     如果陈旧?(:etag =>资源,:last_modified => resource.updated_at.utc)       respond_to do | format |         format.html#show.html.erb           }       结束     端

但访问1.xml会尝试渲染资源两次:

if stale?(:etag => resource, :last_modified => resource.updated_at.utc)
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { 
      render :xml => @order.to_xml(:controller => self, :except => [:paid_at]) 
      }
  end
end

错误消息:

OrdersController中的ActionController :: DoubleRenderError#show

每次操作只能渲染或重定向

RAILS_ROOT:/ Users / guilherme / Documents / ruby​​ / restfulie-test 应用程序跟踪|框架跟踪|完整跟踪

/Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:900:in render_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in ms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:10:in realtime' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in ms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/benchmarking.rb:51:in render' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:1331:in send' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:1331:in perform_action_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/filters.rb:617:in call_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/filters.rb:610:in perform_action_without_benchmark' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in ms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:10:in realtime' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:in ms' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/benchmarking.rb:68:in perform_action_without_rescue' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/rescue.rb:160:in perform_action_without_flash' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/flash.rb:146:in perform_action' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:532:in发送' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:532:in process_without_filters' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/filters.rb:606:in process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:391:in process' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/base.rb:386:in call' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/routing/route_set.rb:437:in`call'

有什么建议吗?

此致

1 个答案:

答案 0 :(得分:0)

不确定你是否找到了解决方案,但我只是遇到了渲染js

的问题

我找到的解决方案是使用逆:

def index
  if stale?(:etag => [request.host],
            :public => true)
    respond_to do |format|
      format.js
    end
  end
end
当然,

用你正在使用的任何东西替换[request.host]以确定新鲜度。