0

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

以下代码适用于 304 且不能进一步渲染: if stale?(:etag => resource, :last_modified => resource.updated_at.utc) respond_to do |format| format.html # show.html.erb } end end

但是访问 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

错误信息:

ActionController::DoubleRenderError in OrdersController#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:inrender' /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:inrealtime' /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:inrender' /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:inperform_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:inperform_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:inms' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4 /lib/active_support/core_ext/benchmark.rb:10:inrealtime' /Library/Ruby/Gems/1.8/gems/activesupport-2.3.4/lib/active_support/core_ext/benchmark.rb:17:inms' /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:inperform_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:insend' /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:inprocess' /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:incall' /Library/Ruby/Gems/1.8/gems/actionpack-2.3.4/lib/action_controller/routing/route_set.rb:437 :在“通话”中

有什么建议么?

问候

4

1 回答 1

0

不确定您是否找到了解决方案,但我在渲染 js 时遇到了同样的问题

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

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

当然,用你用来确定新鲜度的任何东西替换 [request.host]。

于 2010-03-19T00:59:05.333 回答