简体   繁体   English

rails4 + mongoid +载波+ fog + jquery-file-upload-TypeError(没有将nil隐式转换为String)

[英]rails4 + mongoid + carrierwave + fog + jquery-file-upload - TypeError (no implicit conversion of nil into String)

I cannot figure out what I am doing wrong here with a multiple file upload. 我无法弄清楚在多文件上传中我在做什么错。 I have another rails4 app with a similar setup, just with postgresql rather than mongoid and i havent run into this issue. 我有另一个具有类似设置的rails4应用程序,只是使用了postgresql而不是mongoid,而我还没有遇到这个问题。 I am not sure if it's something with carrierwave-mongoid or what. 我不确定这是与载波波状杂音有关的还是什么。 How do i further narrow down what is actually causing the TypeError??? 我如何进一步缩小真正导致TypeError的原因???

Update Recreated entire app and used Postgresql, and running into the same exact issue.. so its not isolated to carrierwave-mongoid like i thought. 更新重新创建了整个应用程序,并使用了Postgresql,并遇到了完全相同的问题..因此,它没有像我想的那样被隔离到载波-杂种动物中。

Development.log Development.log

       Started POST "/images" for 127.0.0.1 at 2014-10-02 11:28:28 -0400
Processing by ImagesController#create as JSON
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MPtXrjrQ+dLb67LVD+aYUrMIYfCUBQWLOaOztlB2sSU=", "image"=>{"gallery_id"=>"3", "file"=>[#<ActionDispatch::Http::UploadedFile:0x007fb55e80c378 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-33694-3bmoib>, @original_filename="10495567_1510476362523625_7774203923998269219_o.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10495567_1510476362523625_7774203923998269219_o.jpg\"\r\nContent-Type: image/jpeg\r\n">]}}
Completed 500 Internal Server Error in 3ms

TypeError (no implicit conversion of nil into String):
  carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:159:in `join'
  carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:159:in `cache_path'
  carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:131:in `block in cache!'
  carrierwave (0.10.0) lib/carrierwave/uploader/callbacks.rb:17:in `with_callbacks'
  carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:122:in `cache!'
  carrierwave (0.10.0) lib/carrierwave/mount.rb:329:in `cache'
  carrierwave (0.10.0) lib/carrierwave/mount.rb:163:in `file='
  carrierwave (0.10.0) lib/carrierwave/orm/activerecord.rb:39:in `file='
  activerecord (4.1.5) lib/active_record/attribute_assignment.rb:45:in `public_send'
  activerecord (4.1.5) lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
  activerecord (4.1.5) lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
  activerecord (4.1.5) lib/active_record/attribute_assignment.rb:26:in `each'
  activerecord (4.1.5) lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
  activerecord (4.1.5) lib/active_record/core.rb:455:in `init_attributes'
  activerecord (4.1.5) lib/active_record/core.rb:198:in `initialize'
  activerecord (4.1.5) lib/active_record/inheritance.rb:30:in `new'
  activerecord (4.1.5) lib/active_record/inheritance.rb:30:in `new'
  cancancan (1.9.2) lib/cancan/controller_resource.rb:85:in `build_resource'
  cancancan (1.9.2) lib/cancan/controller_resource.rb:66:in `load_resource_instance'
  cancancan (1.9.2) lib/cancan/controller_resource.rb:32:in `load_resource'
  cancancan (1.9.2) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
  cancancan (1.9.2) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
  activesupport (4.1.5) lib/active_support/callbacks.rb:440:in `instance_exec'
  activesupport (4.1.5) lib/active_support/callbacks.rb:440:in `block in make_lambda'
  activesupport (4.1.5) lib/active_support/callbacks.rb:160:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:160:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.5) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.5) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.5) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.5) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.5) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.5) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.5) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.5) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.5) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.1.5) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.5) lib/action_controller/metal.rb:232:in `block in action'
  actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `call'
  actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
  actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:50:in `call'
  actionpack (4.1.5) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.5) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.5) lib/action_dispatch/routing/route_set.rb:678:in `call'
  warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.3) lib/warden/manager.rb:34:in `catch'
  warden (1.2.3) lib/warden/manager.rb:34:in `call'
  rack (1.5.2) lib/rack/etag.rb:23:in `call'
  rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
  rack (1.5.2) lib/rack/head.rb:11:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/flash.rb:254:in `call'
  rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.5) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.5) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.5) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.5) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.5) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.5) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.5) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.5) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.5) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.5) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
  rack (1.5.2) lib/rack/runtime.rb:17:in `call'
  activesupport (4.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.1.5) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.5) lib/rails/engine.rb:514:in `call'
  railties (4.1.5) lib/rails/application.rb:144:in `call'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:155:in `handle'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:109:in `rescue in block (2 levels) in start'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:106:in `block (2 levels) in start'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:96:in `each'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:96:in `block in start'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:76:in `loop'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:76:in `start'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/lib/nack/server.rb:12:in `run'
  /Users/joel/Library/Application Support/Pow/Versions/0.4.3/node_modules/nack/bin/nack_worker:4:in `<main>'


  Rendered /usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.5ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.5ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.9ms)
  Rendered /usr/local/rvm/gems/ruby-2.1.2/gems/actionpack-4.1.5/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb (16.2ms)

Model 模型

class Picture
  include Mongoid::Document
  include Mongoid::Timestamps

  belongs_to :gallery

  mount_uploader :file, PictureUploader

  def to_jq_upload
    {
      "name" => read_attribute(:file),
      "size" => file.size,
      "url" => file.url,
      "thumb_url" => file.thumb.url,
      #"delete_url" => gallery_picture_path(:id => id),
      "delete_type" => "DELETE" 
    }
  end
end

Controller 调节器

class PicturesController < ApplicationController
  load_and_authorize_resource

  before_action :set_picture, only: [:destroy]

  def create
    p_attr = params[:picture]
    p_attr[:file] = params[:picture][:file].first if params[:picture][:file].class == Array

    logger.debug "params = #{params}"
    logger.debug "p_attr = #{p_attr}"

    @picture = Picture.new(p_attr)

    if @picture.save
      respond_to do |format|
        format.js
        format.html {  
          render :json => [@picture.to_jq_upload].to_json, 
          :content_type => 'text/html',
          :layout => false
        }
        format.json {  
          render :json => { :files => [@picture.to_jq_upload] }     
        }
      end
    else 
      render :json => [{:error => "custom_failure"}], :status => 304
    end
  end

  private

  def picture_params
      params.require(:picture).permit(:gallery_id, file:[])
  end

Form 形成

    <%= form_for Picture.new, :html => { :multipart => true, :id => "fileupload" } do |fm| %>
            <%= fm.file_field :file, :id => "add_images_field", :multiple => true %>
    <% end %>

Gemfile 的Gemfile

gem 'rails', '4.1.5'
gem 'mongoid', '~> 4.0.0'
gem 'bson_ext'
gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
gem 'mini_magick'
gem 'fog'
gem "jquery-fileupload-rails"

Taking a quick look I'd say you've set your picture_params incorrectly; 快速浏览一下,我会说您设置的picture_params错误; at least based on the POST submission string. 至少基于POST提交字符串。 "file" is nested under "image", it isn't in the root level of the hash. “文件”嵌套在“图像”下,它不在哈希的根目录下。

Rails 4 - Strong Parameters - Nested Objects Rails 4-强参数-嵌套对象

Here's some visual formatting for your POST string: 这是您的POST字符串的一些可视格式:

{
"utf8"=>"✓"
"authenticity_token"=>"MPtXrjrQ+dLb67LVD+aYUrMIYfCUBQWLOaOztlB2sSU="
"image"=>{
    "gallery_id"=>"3"
    "file"=>[#<ActionDispatch::Http::UploadedFile:0x007fb55e80c378 @tempfile=#<Tempfile:/var/folders/3m/t1v11gzj32n0fdbhwr823y600000gn/T/RackMultipart20141002-33694-3bmoib>
        @original_filename="10495567_1510476362523625_7774203923998269219_o.jpg"
        @content_type="image/jpeg"
        @headers="Content-Disposition: form-data; name=\"image[file][]\"; filename=\"10495567_1510476362523625_7774203923998269219_o.jpg\"\r\nContent-Type: image/jpeg\r\n">]
}
}

So what you'd need would be: 因此,您需要的是:

params.require(:picture).permit(:image => [:gallery_id, file: []])

However since your form submission hash doesn't match the name of your model - for whatever reason, try: 但是,由于您的表单提交哈希与模型名称不匹配-出于任何原因,请尝试:

params.require(:image).permit(:gallery_id, file: [])

EDIT: I'm also curious about why you're nesting the file and gallery_id attributes inside image . 编辑:我也很好奇为什么您要在image嵌套filegallery_id属性。 That will change the way rails assigns the parameters to the model and perhaps is why you're getting nil. 这将改变Rails将参数分配给模型的方式,这也许就是为什么您得到零的原因。 I think the real problem is the way you've set up your form. 我认为真正的问题是您建立表格的方式。 Take a look at your routes.rb file; 查看您的routes.rb文件; the URL ends in /images , however your model is Picture . URL以/images结尾,但是您的模型是Picture It seems you have a conflict of nomenclature - probably in multiple areas of your code. 看来您在命名方面有冲突-可能在代码的多个区域中。

@JakeTheSnake may be onto something. @JakeTheSnake可能在某些地方。 On a more specific level, I took a quick look myself, based on your backtrace. 在更具体的层面上,我根据您的回溯来快速浏览了一下自己。

See the source code here on GitHub , line 159, which is the line that the first two lines of your backtrace reference: 在GitHub的第159行中查看源代码,这是backtrace的前两行引用的行:

carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:159:in `join'
carrierwave (0.10.0) lib/carrierwave/uploader/cache.rb:159:in `cache_path'

Grains of salt, here, because I know nothing about Carrierwave and very little information to go off of. 这里是盐粒,因为我对Carrierwave一无所知,而且很少有信息可做。 However, I searched the 0.10.0 carrierwave docs for method "cache_path", and found nothing. 但是,我在0.10.0载波文档中搜索了方法“ cache_path”,却一无所获。 Note, the first line of your backtrace references "join", as in File.join. 注意,回溯的第一行引用File.join中的“ join”。 My guess is that one of the three calls within that File.join, cache_name , cache_path , or the final root are returning nil. 我的猜测是,该File.join中的三个调用之一, cache_namecache_path或最后一个root都返回nil。 File.join is trying to convert this nil into a String , which will not happen in Ruby. File.join试图将此nil转换为String ,这在Ruby中不会发生。

I know this answer is not as technical as it probably could be, and that it doesn't come close to directly solving the issue. 我知道这个答案并不像它可能的那么技术性,而且还不能直接解决问题。 I only hope that it can at least point you in the right direction or help in some way. 我只希望它至少可以为您指明正确的方向或以某种方式提供帮助。

Please let me know if I can be of any more assistance. 请让我知道是否可以提供更多帮助。 Good luck. 祝好运。

EDIT: It could be anywhere, I've gathered from looking into more of the code referenced in the backtrace, leading me to believe @JakeTheSnake really is onto something there. 编辑:可能在任何地方,我从调查回溯引用的更多代码中汲取了灵感,使我相信@JakeTheSnake确实存在于其中。 I believe that he is probably right when he says it is something high-level (like a misconfigured form or controller, something small), however, deeply analyzing your backtraces and the code behind them is super, super helpful and can yield many pieces of the puzzle. 我认为当他说这是一个高级问题(例如配置错误的表单或控制器,有些小的东西)时,他可能是对的,但是,深入分析您的回溯和背后的代码是非常有用的,并且会产生很多难题。 I'd take a good, hard look at the code referenced in the backtrace, if you have not already. 如果您还没有的话,我将认真回顾一下回溯中引用的代码。 Again, good luck, and let me know if I can help more. 再次祝您好运,请告诉我是否可以提供更多帮助。

This is my solution: 这是我的解决方案:
Model: 模型:

class Picture < ActiveRecord::Base
  include Rails.application.routes.url_helpers
  mount_uploader :image, SimpleImageUploader

  def to_jq_upload
  {
    "name" => read_attribute(:image),
    "size" => image.size,
    "url" => image.url,
    "thumbnail_url" => image.thumb.url,
    "delete_url" => picture_path(:id => id),
    "delete_type" => "DELETE"
  }
  end
end

Action: 行动:

  def create
    pics = picture_params
    @picture = Picture.new(image: pics[:image].first)
    if @picture.save
       ...
    end
  end
  ...
  private
  def picture_params
    params.require(:picture).permit(image: [])
  end

Shortly view: 简短查看:

<%= form_for Picture.new, :html => {:multipart => true, :id => "fileupload"} do |f| %>
  <%= f.file_field :image, multiple: true, accept: 'image/*' %>
  ..
<% end %>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails,mongoid抛出TypeError不会将nil隐式转换为String - Rails, mongoid throws TypeError no implicit conversion of nil into String Carrierwave:`eval&#39;:没有将nil隐式转换为String(TypeError) - Carrierwave: `eval': no implicit conversion of nil into String (TypeError) Rails 5 Fog CarrierWave 照片上传到 Amazon S3 没有将字符串隐式转换为数组 - Rails 5 Fog CarrierWave Photo Upload to Amazon S3 no implicit conversion of String into Array 在Heroku上出现CarrierWave和S3的Rails错误,TypeError:没有将nil隐式转换为String - Rails error with CarrierWave and S3 on Heroku, TypeError: no implicit conversion of nil into String 对于载波多图像上传,“没有将nil隐式转换为字符串”错误 - “no implicit conversion of nil into String” error for carrierwave multiple image upload 在Rails 5上的TypeError(没有将nil隐式转换为String)ruby - TypeError (no implicit conversion of nil into String) ruby on rails 5 类型错误:没有将 nil 隐式转换为 String Heroku 和 Rails - TypeError: no implicit conversion of nil into String Heroku and Rails Rails 6.0.0 TypeError - 没有将 nil 隐式转换为字符串: - Rails 6.0.0 TypeError - no implicit conversion of nil into String: TypeError(没有将 nil 隐式转换为 String): - TypeError (no implicit conversion of nil into String): rails db schema sql格式TypeError:没有将nil隐式转换为String - rails db schema sql format TypeError: no implicit conversion of nil into String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM