Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
RubyonRails上传参数为nil_Ruby_Ruby On Rails 4_Paperclip_Dropzone.js - Fatal编程技术网

RubyonRails上传参数为nil

RubyonRails上传参数为nil,ruby,ruby-on-rails-4,paperclip,dropzone.js,Ruby,Ruby On Rails 4,Paperclip,Dropzone.js,我正在尝试使用dropzone和曲别针在RubyonRails中上传一个文件。我有以下代码: 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] F

我正在尝试使用dropzone和曲别针在RubyonRails中上传一个文件。我有以下代码:

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">&times;</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>

我完全无法理解为什么我的params对象为零。有人知道我做错了什么吗?我已经在这里讨论了几十个问题,但没有一个有效。

在JS中,您将参数名称设置为
“model[:upload]”
-符号在JS中不存在。你可以在Rails日志中看到,该文件以“:upload”的形式提交。天哪,我花了将近5个小时查看了这个文件,就这样。非常感谢你。如果可以的话,我会给你买杯啤酒。提示:使用
Rails.logger.debug
而不是
put
。它显示在
log/development.log
中。
//= 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")
  })

});