我正在使用Rails Carrier Wave与
JQuery上传,就像这个
rails tutorial,但是当我点击上传按钮时我有一个错误:
@H_403_7@解决方法
Error: SyntaxError: JSON.parse
任何建议/建议是非常好的.
为什么不尝试
Uploadify?
步骤1
将宝石’carrier_wave’添加到您的Gemfile.
第2步
将此代码保存到/lib/flash_session_cookie_middleware.rb中:
require 'rack/utils' class FlashSessionCookieMiddleware def initialize app,session_key = '_session_id' @app = app @session_key = session_key end def call env if env['HTTP_USER_AGENT'] =~ /^(Adobe|Shockwave) Flash/ request = Rack::Request.new env env['HTTP_COOKIE'] = [@session_key,request.params[@session_key]].join('=').freeze unless request.params[@session_key].nil? env['HTTP_ACCEPT'] = "#{request.params['_http_accept']}".freeze unless request.params['_http_accept'].nil? end @app.call env end end
步骤3
编辑session_store.rb并将此代码添加到文件的末尾:
Rails.application.config.middleware.insert_before( ActionDispatch::Session::CookieStore,FlashSessionCookieMiddleware,Rails.application.config.session_options[:key] )
步骤4
下载Uploadify并解压缩.
步骤5
>将jquery.uploadify.v2.1.4.min.js和swfobject.js复制到
/ app / assets / javascripts如果您使用Rails 3.1或更高版本;至
/ public / javascripts,如果您使用Rails 3.0或更早版本.
将uploadify.swf和cancel.png复制到/ app / assets / images /或
/大众/图像.
>将uploadify.css复制到/ app / assets / stylesheets /或
/公共/样式表.
步骤6
编辑你的application.js并插入下面:
//= require swfobject //= require jquery.uploadify
步骤7
<input id="uploadify" name="uploadify" type="file"/>
步骤8
$(document).ready(function() { <% key = Rails.application.config.session_options[:key] %> var uploadify_script_data = {}; var csrf_param = $('Meta[name=csrf-param]').attr('content'); var csrf_token = $('Meta[name=csrf-token]').attr('content'); uploadify_script_data[csrf_param] = encodeURI(encodeURIComponent(csrf_token)); uploadify_script_data['<%= key %>'] = '<%= cookies[key] %>'; $('#uploadify').uploadify({ uploader : '/assets/uploadify.swf',script : '/photos',cancelImg : '/images/cancel.png',auto : true,multi : true,removeCompleted : true,scriptData : uploadify_script_data,onComplete : function(event,ID,fileObj,doc,data) { } }); });
步骤9
写下你的控制器:
def create @photo = Photo.new image: params[:file_data] @photo.save end
注意:已通过Uploadify 2.1.4进行了测试.