【问题标题】:uploadify and paperclip on rails 3.1在rails 3.1上上传和回形针
【发布时间】:2012-05-16 23:48:23
【问题描述】:

我正在尝试使用 uploadify 和回形针简单地上传文件。

这是索引视图:

<%- session_key = Rails.application.config.session_options[:key] -%>

$('#upload').uploadify({
    'uploader'  : 'uploadify.swf',
    'script'    : '/videos',
    'cancelImg' : 'images/cancel.png',
    'buttonText'  : 'Add video!',
    'folder'    : '/public',
    'auto'      : true,
    'scriptData' : {"<%= key = Rails.application.config.session_options[:key] %>" : "<%= cookies[key] %>",
        "<%= request_forgery_protection_token %>" : "<%= form_authenticity_token %>",
    },
    onError : function (event, id, fileObj, errorObj) {
        alert("error: " + errorObj.info);
    }
});

这是我的模型:

class Video < ActiveRecord::Base

  has_attached_file :source

  validates_attachment_presence :source

end

...这是我的迁移文件:

class CreateVideos < ActiveRecord::Migration
 def self.up
  create_table :videos do |t|
   t.string :source_content_type
   t.string :source_file_name
   t.integer :source_file_size
   t.timestamps
 end
end

 def self.down
  drop_table :videos
 end
end

现在,当我上传文件时,无论是客户端还是服务器端都没有出现错误,但看起来回形针没有收到 uploadify 发送给他的参数。事实上,如果我从模型中删除验证,资源就会在没有参数的情况下创建。

在控制器中我添加:

def create
 logger.info(params.inspect) (1)
 @video = Video.new(params[:video])
 @video.save
 logger.info @video.inspect (2)
end

在 development.log (1) 中给了我

{"Filename"=>"prova.mov", "authenticity_token"=>"9q5w5LipGoBj Iac055OPbDofFU5FMbpEX675 RqOqs=", "folder"=>"/public", "_StreamVideo_Api_session"=>...

(2) 未经验证

#<Video id: 1, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: "2011-10-10 12:12:38", updated_at: "2011-10-10 12:12:38">

(2) 有验证

#<Video id: nil, source_content_type: nil, source_file_name: nil, source_file_size: nil, created_at: nil, updated_at: nil>

问题可能出在哪里? 提前感谢您的回答!

我认为:

<h1>New video</h1>

<% form_for :video, :html => { :multipart => true } do |f| %>
  <%= f.file_field :source%>
<% end %>

但是http://localhost:3000/videos/new 只显示了标题!!

【问题讨论】:

    标签: paperclip ruby-on-rails-3.1 uploadify


    【解决方案1】:

    代码看起来没问题,你应该在那里添加encodeURI 函数。在 Uploadify 2.1 中 它应该适用于:

     'scriptData' : {
         $('meta[name=csrf-param]').attr('content') : encodeURI(encodeURIComponent($('meta[name=csrf-token]').attr('content')))
      }
    

    使用 Uploadify 3.1:

    'formData': {
             $('meta[name=csrf-param]').attr('content') : encodeURI($('meta[name=csrf-token]').attr('content'))
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-10
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      • 2015-05-09
      相关资源
      最近更新 更多