2010-04-22 58 views
4

我想要使用uploadify(http://www.uploadify.com)在一个rails 3应用程序中使用文件上传进度条,但我卡在真实性标记中。我现在uploadify的配置看起来像Uploadify和rails 3真实性标记

  <script type="text/javascript" charset="utf-8"> 
      $(document).ready(function() { 
        $("#zip_input").uploadify({ 
        'uploader': '/flash/uploadify.swf', 
        'script': $("#upload").attr('action'), 
        'scriptData': { 'format': 'json', 'authenticity_token': encodeURIComponent('<%= form_authenticity_token if protect_against_forgery? %>') }, 
        'fileDataName': "world[zip]", 
        //'scriptAccess': 'always', // Incomment this, if for some reason it doesn't work 
        'auto': true, 
        'fileDesc': 'Zip files only', 
        'fileExt': '*.zip', 
        'width': 120, 
        'height': 24, 
        'cancelImg': '/images/cancel.png', 
        'onComplete': function(event, data) { $.getScript(location.href) }, // We assume that we can refresh the list by doing a js get on the current page 
        'displayData': 'speed' 
        }); 
       }); 
      </script> 

但我正在逐渐从铁轨这样的响应:

Started POST "/worlds" for 127.0.0.1 at 2010-04-22 12:39:44 

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken): 


Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms) 
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (6.6ms) 
Rendered /opt/local/lib/ruby/gems/1.8/gems/actionpack-3.0.0.beta3/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (12.2ms) 

这似乎是因为我没有与请求一起发送身份验证cookie。有谁知道我该如何获得我应该在那里发送的值,以及如何让rails从HTTP POST中读取它,而不是试图将其作为cookie发现?

回答

3

跳过真实性标记检查并不理想,因为它打开XSS攻击媒介。 另一种使这项工作的方法在这里描述:http://metautonomo.us/2010/07/09/uploadify-and-rails-3/

请注意,您可能需要加倍网址编码的东西。在这个例子中,使用rails'u'和encodeURLComponent()。但是,如果你有更多的fancy/rails3类型设置并从页眉中的元标签获取会话数据/真实性令牌,你需要调用encodeURLComponent()两次。

+0

我对authenticity_token being带'+'字符的严重问题。你的建议解决了我的问题。做双u()是关键。 escapeURIComponent和u()的工作方式有什么不同? – mdrozdziel 2010-08-05 09:43:18

2

好吧,我想如何去解决这个问题。视图中是否有表单需要上传文件。如果你只是使用jQuery获取隐藏真实性标记的值并将其传递到scriptData var。

var token = ($('input[name=authenticity_token]').val()); 
scriptData : {'authenticity_token':token} 

希望这适用于你。

+0

感谢您的建议,我还没有机会重新审视这个项目,但您的建议看起来非常有用,一旦我有机会得到这个工作,我会回到你身边。 – Ceilingfish 2010-05-14 09:09:07