You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Dropzone开发类DropBox平台时,修改UploadsController后遇困境

Hey there! Let's sort out that UploadsController snag you're hitting while building your Dropbox-like platform with Dropzone, Rails, and Ajax. Your code cut off mid-way, so first let's fix the incomplete create action and make sure it works seamlessly with Dropzone's Ajax workflow.

First, Fix the Controller Code

Your original create method had a syntax error (:status =&...) and was missing error handling—both critical for Dropzone to understand if an upload succeeded or failed. Here's a complete, working version:

class UploadsController < ApplicationController
  def new
    @upload = Upload.new
  end

  def create
    # Use new + save instead of create to better handle errors
    @upload = Upload.new(upload_params)
    
    if @upload.save
      # Return a RESTful success response with useful data
      render json: { 
        message: "Upload successful!", 
        upload_id: @upload.id,
        file_url: @upload.file.url # Adjust this to match your Paperclip attachment name
      }, status: :created
    else
      # Return error details so Dropzone can notify the user
      render json: { 
        error: @upload.errors.full_messages.join(", ") 
      }, status: :unprocessable_entity
    end
  end

  private

  def upload_params
    # Replace :file with your actual Paperclip attachment attribute name
    # Add any other permitted fields your Upload model uses
    params.require(:upload).permit(:file, :title, :description)
  end
end

Key Fixes & Explanations

  • Syntax & Error Handling: Swapped Upload.create for Upload.new + explicit save so we can catch validation errors and return meaningful feedback to Dropzone.
  • Proper HTTP Status Codes:
    • Return :created (201) on success—this is the RESTful standard for creating resources.
    • Return :unprocessable_entity (422) on failure, which tells Dropzone the upload failed.
  • Useful JSON Response: Include details like the upload ID or file URL so your frontend can display or reference the uploaded file later.
  • Strong Params: Make sure upload_params includes your Paperclip attachment field (e.g., :file if your model has has_attached_file :file) and any other fields your Upload model requires.

Frontend Dropzone Configuration Check

Make sure your Dropzone setup points to the correct route and handles success/error responses:

Dropzone.options.yourDropzoneElementId = {
  url: '/uploads', // Matches your routes.rb for uploads#create
  acceptedFiles: 'image/*,application/pdf,application/vnd.openxmlformats-officedocument.wordprocessingml.document', // Adjust allowed file types
  maxFilesize: 10, // Max file size in MB
  success: function(file, response) {
    file.previewElement.classList.add('dz-success');
    console.log(response.message);
    // Add logic here to display the uploaded file (e.g., append a link to response.file_url)
  },
  error: function(file, response) {
    file.previewElement.classList.add('dz-error');
    alert(`Upload failed: ${response.error}`);
  }
};

Quick Route Check

Double-check your config/routes.rb has the necessary route for uploads:

resources :uploads

That should get your upload flow working smoothly again. If you run into specific issues (like Paperclip attachment errors or Dropzone not triggering the right callbacks), feel free to share more details!

内容的提问来源于stack exchange,提问作者Chaalhar Oz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:39:50