【问题标题】:RoR: using Dropzone.js with jQuery sortableRoR:使用带有 jQ​​uery 可排序的 Dropzone.js
【发布时间】:2015-03-09 05:05:00
【问题描述】:

我在 Rails 应用程序中结合了 Dropzone Js 和回形针,我还设法将 jquery 的可排序应用到它。

我的目标是:

  1. 能够将每个图像的位置保存到数据库 上传成功
  2. 当我更改(排序)图像的顺序时,更新数据库中每个图像的位置。

我的上传模型有 :image 和 :position 我的路线有集合 {post "sort"}

我的控制器代码:

class UploadsController < ApplicationController

  def new
    @upload = Upload.new
  end

  def upload_list
    @uploads = Upload.all.select(:id, :image_file_name, :image_file_size).order("position")

    respond_to do |format|
      format.json {render json: @uploads.to_json(methods: [:path])}
    end
  end

  def create
    @upload = Upload.create(upload_params)
    @upload.position = params[:upload][:position]

    if @upload.save
      render json: { message: "success", fileID: @upload.id }, :status => 200
    else
      render json: { error: @upload.errors.full_messages.join(',')}, :status => 400
    end
  end

  def sort
    //params[:upload] gives undefined method for nil error 
    Upload.all.each_with_index do |id, index|
      Upload.where(id: id).update_all({position: index+1})
    end
    render nothing: true
  end

  def destroy
    @upload = Upload.find(params[:id])
    if @upload.destroy
      render json: { message: "File deleted from server" }
    else
      render json: { message: @upload.errors.full_messages.join(',') }
    end
  end

  private
    def upload_params
      params.require(:upload).permit(:image, :position)
    end
end  

上传.rb:

class Upload < ActiveRecord::Base

  has_attached_file :image, :styles => { :medium => "300x300>",:thumb => "100x100>" }

  validates_attachment :image,
                   :presence => true,
                   :content_type => { :content_type => /\Aimage\/.*\Z/ },
                   :size => { :less_than => 1.megabyte } 

  def path
    image.url
  end

  def as_json(options = { })
    h = super(options)
    h["name"] = h.delete "image_file_name"
    h["size"] = h.delete "image_file_size"
    h
  end

end 

new.html.erb:

<h1>Uploads#new</h1>

<%= form_for(@upload, html: { multipart: true, class: "dropzone"}) do |f| %>
  <div class="fallback">
    <%= f.file_field :image %>
    <%= f.submit "Upload" %>
  </div>
<% end %>  

最后上传.js:

$(document).ready(function(){

  Dropzone.autoDiscover = false;

  $("#new_upload").dropzone({
    maxFilesize: 1,
    paramName: "upload[image]",
    addRemoveLinks: true,
    dictRemoveFile: "Delete",

    //to show existing images from db 
    init: function() {
      var thisDropZone = this;
      $.getJSON('upload_list', function(data) {
        $.each(data, function(index, val) {
          var mockFile = { name: val.name, size: val.size };
          thisDropZone.emit("addedfile", mockFile);
          thisDropZone.emit("thumbnail", mockFile, val.path);
          $(mockFile.previewTemplate).find('.dz-remove').attr('id', val.id);

          // adding id attribute for serialize
          $(".dz-preview:last-child").attr('id', "image_" + val.id);
        });
      });
    },

    success: function(file, response){
      $(file.previewTemplate).find('.dz-remove').attr('id', response.fileID);
      $(file.previewElement).addClass("dz-success");

      var order = $('.dropzone').sortable('serialize');
      $.ajax({
        type: 'POST',
        url: '/uploads/sort',
        data: order,
        success: function(data){
          console.log(data);
        }        
      });
    },

    removedfile: function(file){
      var id = $(file.previewTemplate).find('.dz-remove').attr('id');
      file.previewElement.remove();

      $.ajax({
        type: 'DELETE',
        url: '/uploads/' + id,
        success: function(data){
          console.log(data.message);
        }
      });

      var order = $('.dropzone').sortable('serialize');
      $.ajax({
        type: 'POST',
        url: '/uploads/sort',
        data: order,
        success: function(data){
          console.log(data);
        }        
      });
    }
  });

 //this function is for sorting + updating positions of old images loaded by the init function.
  $(function() {
    $(".dropzone").sortable({
      items:'.dz-preview',
      cursor: 'move',
      opacity: 0.5,
      containment: '.dropzone',
      distance: 20,
      update: function(event, ui) {
        var order = $('.dropzone').sortable('serialize');
        $.ajax({
          type: 'POST',
          url: '/uploads/sort',
          data: order,
          success: function(data){
            console.log(data);
          }        
        });
      }
    });
    $(".dropzone").disableSelection();
  });
}); 

我知道我有很多清理工作要做,但我正在努力让它先发挥作用。

我知道我需要在每个图像的 .dz-preview 的 dropzone 中添加一个以“_”开头的 id,以便序列化工作。然而,成功函数中的 ajax 帖子在没有应用任何 id 属性的情况下工作正常,所以当我上传 3 张图片时,在 db 中的位置分别为 1、2 和 3,如果我删除第二张图片,removedfile 函数也可以工作,由于控制器中的排序方法,image1 获得了位置“1”,而 image3 的位置属性更新为 2。

我尝试的是在 init 函数中添加一行以将 id 属性附加到 .dz-preview 并刷新页面,以便我测试 init 函数并再次尝试排序,但它是相同的,我得到的是:

Started POST "/uploads/sort" for 127.0.0.1 at 2015-01-12 12:47:39 +0200
Processing by UploadsController#sort as */*
Parameters: {"image"=>["318", "320", "319"]}
Upload Load (2.7ms)  SELECT "uploads".* FROM "uploads"
SQL (5.4ms)  UPDATE "uploads" SET "position" = 1 WHERE "uploads"."id" = 318
SQL (14.7ms)  UPDATE "uploads" SET "position" = 2 WHERE "uploads"."id" = 319
SQL (1.8ms)  UPDATE "uploads" SET "position" = 3 WHERE "uploads"."id" = 320
Rendered text template (0.1ms)
Completed 200 OK in 54ms (Views: 2.0ms | ActiveRecord: 29.6ms)

注意参数的顺序,所以图像“318”应该占据位置 1,“320”应该占据位置 2,而“319”应该占据位置 3...但是如图所示,更新中忽略了新顺序。 .

任何想法我错过了什么?是 sort 方法还是 .sortable 函数?

谢谢

【问题讨论】:

    标签: jquery ruby-on-rails jquery-ui-sortable dropzone.js


    【解决方案1】:

    使用以下代码更新您的排序操作。

      def sort
        images = Upload.where(id: params[:image])
        images.each do |image|
          if position = params[:image].index(image.id.to_s)
            image.update_attribute(:position, position + 1) unless image.position == position
          end 
        end 
        render nothing: true
      end 
    

    【讨论】:

    • 是有道理的,但现在看来该方法没有得到任何ID?参数:{"image"=>["319", "320", "318"]} 上传负载 (0.4ms) SELECT "uploads".* FROM "uploads" WHERE "uploads"."id" IS NULL跨度>
    • 更新答案,应该是 params[:image] 而不是 params[:upload]
    • 我在 if 语句中添加了一个额外的“=”,但我也注意到 position 没有定义,我认为你的意思是 if image.position 但我认为不是这样,因为你比较图像。在 if 语句中的位置与位置...那么位置应该定义为什么?
    • 我很惊讶代码正在更新排序,尽管没有定义位置和 if 语句中的单个“=”,请您向我解释一下吗?现在还有一个问题,当我第一次上传图像时,位置没有插入到数据库中。它仅在我刷新页面以检索图像并开始排序时才有效...
    • 我将位置变量分配给图像 id 的索引,不要将其更改为比较
    猜你喜欢
    • 2011-05-08
    • 1970-01-01
    • 2011-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多