Uploadify和rails 3真实性令牌

时间:2010-04-22 11:48:42

标签: ruby-on-rails cookies uploadify ruby-on-rails-3

我正在尝试使用uploadify(http://www.uploadify.com)在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>

但是我从rails获得了这个回复:

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 个答案:

答案 0 :(得分:3)

这似乎是rails 3的一个错误。

https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3913

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

更改
protect_from_forgery :except => :upload

skip_before_filter :verify_authenticity_token, :only => :upload

这似乎仍然可以正常工作

答案 1 :(得分:3)

答案 2 :(得分:2)

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

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

希望这适合你。