我正在使用JQuery插件(Plupload)在我的网站上上传多个文件。上传完成后,我会将用户重定向到一个编辑页面,在那里他们可以标记并命名他们刚刚上传的照片。我可以访问上传的文件数量(通过调用存储文件的数组的长度),并希望在我的照片控制器中设置一个实例变量,以便我可以提取刚刚上传的文件。
重定向正在运行,日志显示参数传递正确的整数。以下是日志显示的内容:
Started POST "/photos?files=1" for 127.0.0.1 at 2012-03-12 23:09:17 -0400
Processing by PhotosController#create as JSON
Parameters: {"files"=>"1"}
以下是脚本中的重定向和后期操作:
//redirect after complete
function attachCallbacks(uploader) {
uploader.bind('FileUploaded', function(Up, File, Response) {
if( (uploader.total.uploaded + 1) == uploader.files.length)
{
var target = "/photos";
var filesAdded = uploader.files.length;
$.ajax({
type: 'post',
url: target + '?files='+filesAdded,
dataType: 'json'
});
window.location = "<%=j photos_path %>";
}
})
}
这是photos_controller.rb索引操作,我尝试使用参数设置变量:
class PhotosController < ApplicationController
respond_to :html, :json
def index
@data = params[:files]
@user = current_user
@photos = current_user.photos
@photo = Photo.new
end
我想限制视图中的照片,只显示刚刚添加的内容:
@photos = current_user.photos.limit(@data)
谢谢,如果您需要更多信息,请与我们联系。
答案 0 :(得分:1)
完全不打算在控制器中使用变量 -
重定向到photos#index
时,如何在脚本中传递文件长度:
window.location = "<%=j photos_path %>"+ '?files=' + filesAdded;
即。重定向到/photos?files=3
,已添加3个文件
然后捕获files
参数并限制要在photos#index
中呈现的照片:
@photos = current_user.photos.order('created_at DESC').limit(params[:files]) if params[:files].present?