Ruby on rails Uploadify和rails 3真实性令牌

Ruby on rails Uploadify和rails 3真实性令牌,ruby-on-rails,cookies,uploadify,ruby-on-rails-3,Ruby On Rails,Cookies,Uploadify,Ruby On Rails 3,我正在尝试使用uploadify()在rails 3应用程序中创建一个文件上传进度条,但我一直在使用真实性标记。我当前的uploadify配置看起来像 <script type="text/javascript" charset="utf-8"> $(document).ready(function() { $("#zip_input").uploadify({

我正在尝试使用uploadify()在rails 3应用程序中创建一个文件上传进度条,但我一直在使用真实性标记。我当前的uploadify配置看起来像

            <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>

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

嗯,我想了一下如何解决这个问题。视图中是否有要上载文件的表单。如果您使用jquery获取隐藏的真实性令牌的值,并将其传递到scriptData变量

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

希望这对您有用。

这似乎是rails 3的一个bug

这意味着我必须改变跳过真实性令牌检查的方式:

protect_from_forgery :except => :upload


这似乎仍然可以正常工作

跳过真实性令牌检查并不理想,因为它会打开XSS攻击向量。 另一种实现此功能的方法如下所述:


请注意,您可能需要将url编码加倍。在本例中,使用了rails“u”以及encodeURLComponent()。但是,如果您设置了更高级的/rails3类型,并从页面标题中的元标记中获取会话数据/真实性令牌,则需要调用encodeURLComponent()两次。

感谢您的建议,我还没有机会重新访问此项目,但您的建议看起来非常有用,一旦我有机会让它工作起来,我会回来找你的。我对真伪有严重的问题,因为我把“+”字符去掉了。你的建议解决了我的问题。执行双u()是关键。escapeURIComponent和u()的工作方式有什么不同吗?
protect_from_forgery :except => :upload
skip_before_filter :verify_authenticity_token, :only => :upload