我正在尝试使用dropzone和paperclip在Ruby on Rails上传文件。我有以下代码:
def create
puts "MOdel:"
puts Model.column_names
@model = Model.new(model_params)
puts "params:"
puts (params[:model])[:upload].inspect
uploaded_io = params[:model][:upload]
File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
file.write(uploaded_io.read)
end
# @model.upload_file_name = @model.upload.filename
# @model.upload_content_type = @model.upload.content_type
# FileUtils.mv(@model.upload.tempfile, "public/uploads/" + @model.id)
respond_to do |format|
if @model.save
puts "successfully saved model"
format.html { redirect_to @model, notice: 'Model was successfully created.' }
format.json { render :show, status: :created, location: @model }
else
format.html { render :new }
format.json { render json: @model.errors, status: :unprocessable_entity }
end
end
end
...
def model_params
params.require(:model).permit(:upload)#:upload => [:filename, :content_type, :upload_file_size, :created_at])
end
我服务器的输出如下:
Started POST "/models" for 73.34.17.74 at 2017-03-12 23:48:37 +0000
Cannot render console from 73.34.17.74! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255
Processing by ModelsController#create as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sE2aEnxC8K33r+s/oPOrz7HtslQq3HO6ipn971wShOsghbzp/u2qKNSXiNRQ0btP2EqA0pIvMhfBwTxVS+Bz6g==", "model"=>{":upload"=>#<ActionDispatch::Http::UploadedFile:0x007f1d5c130d78 @tempfile=#<Tempfile:/tmp/RackMultipart20170312-9855-routhl.jpg>, @original_filename="3hajXfT.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"model[:upload]\"; filename=\"3hajXfT.jpg\"\r\nContent-Type: image/jpeg\r\n">}}
MOdel:
id
upload_file_name
upload_content_type
upload_file_size
upload_updated_at
created_at
updated_at
Unpermitted parameter: :upload
params:
nil
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `original_filename' for nil:NilClass):
app/controllers/models_controller.rb:45:in `create'
我还有一个看起来像这样的视图文件:
<div class="overlay hidden">
<div class="row overlay-inner">
<div class="row">
<button type="button" class="close-uploads" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="row">
<%= form_for(@model, html: { multipart: true, class: "dropzone"}) do |f| %>
<div class="fallback">
<%= f.file_field :upload %><br>
<%= f.submit "Upload my file" %>
</div>
<% end %>
</div>
<div class="index row">
<%= render "uploads/helper.html.erb" %>
</div>
</div>
</div>
我的uploads.js文件如下:
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require dropzone
//= require_tree .
$(document).ready(function(){
// This handles the actual upload js
// disable auto discover
Dropzone.autoDiscover = false;
var dropzone = new Dropzone (".dropzone", {
maxFilesize: 256, // Set the maximum file size to 256 MB
paramName: "model[:upload]", // Rails expects the file upload to be something like model[field_name]
addRemoveLinks: false // Don't show remove links on dropzone itself.
});
dropzone.on("success", function(file) {
this.removeFile(file)
$.getScript("/upload")
})
});
我完全被困在这里,为什么我的params对象为零。谁能看到我在这里做错了什么?我在这里已经讨论了几十个问题,但这些都没有用。