【问题标题】:Rails4 Mongoid Paperclip does not upload file, but everything elseRails4 Mongoid Paperclip不上传文件,但其他一切
【发布时间】:2014-02-19 12:10:38
【问题描述】:

我有一个使用 Mongoid 和 Mongoid-Paperclip 的 Rails4 项目。 我刚刚设置了 ImageMagick,命令似乎有效。 保存图像的模型被创建,所有图像字段都被填充。 唯一不起作用的是将文件复制到 public/system/... 文件夹(在我的 Mac 上处于开发模式)。

这是代码和输出:

目录图像(由零件嵌入)

class CatalogImage
  include Mongoid::Document
  include Mongoid::Paperclip

  field :caption

  embedded_in :part, inverse_of: :catalog_images
  has_mongoid_attached_file :image,
      :path => ":rails_root/public/system/:attachment/:id/:style/:filename",
      :url => "/system/:attachment/:id/:style/:filename",
      :styles => { :small => "200x200>", :medium => "500x500>" }
  validates_attachment_content_type :image, :content_type => %w(image/jpeg image/jpg image/png)
end

部分(嵌入许多目录图像)

class Part
  include Mongoid::Document
  field :name
  field :description
  field :price
  field :clickout

  embeds_many :catalog_images

  accepts_nested_attributes_for :catalog_images

end

宝石文件

...
gem 'mongoid', '4.0.0.beta1', github: 'mongoid/mongoid'
gem 'bson_ext'

gem "mongoid-paperclip", :require => "mongoid_paperclip", :git => "git://github.com/meskyanichi/mongoid-paperclip.git", :branch => "develop"
...

输出

Started POST "/parts" for 127.0.0.1 at 2014-02-18 23:19:02 +0100
Processing by PartsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"io4UetDC8hkUxDzJsZWSL639P8fTcEvrQ9HyEjPc5x0=", "part"=>{"name"=>"FOE", "description"=>"", "price"=>"", "clickout"=>"", "catalog_images_attributes"=>{"0"=>{"caption"=>"", "image"=>#<ActionDispatch::Http::UploadedFile:0x007f8b3bf79ec8 @tempfile=#<Tempfile:/tmp/RackMultipart20140218-3774-mrhq7e>, @original_filename="GOPR0041.JPG", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"part[catalog_images_attributes][0][image]\"; filename=\"GOPR0041.JPG\"\r\nContent-Type: image/jpeg\r\n">}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}, "user_images_attributes"=>{"0"=>{"caption"=>""}, "1"=>{"caption"=>""}, "2"=>{"caption"=>""}}}, "commit"=>"Create Part"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} runtime: 0.8190ms
  MOPED: 127.0.0.1:27017 QUERY        database=parti_development collection=users selector={"$query"=>{"_id"=>BSON::ObjectId('53013f31656c2d8c48010000')}, "$orderby"=>{:_id=>1}} flags=[] limit=-1 skip=0 batch_size=nil fields=nil runtime: 18.5110ms
USER ROLE: admin
Command :: file -b --mime-type '/tmp/2a5dd22ac67a08cdb0f94370de8f2c2d20140218-3774-du97lf'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]' 2>/dev/null
Command :: identify -format %m '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]'
Command :: convert '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]' -auto-orient -resize "200x200>" '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx420140218-3774-17toiy6'
Command :: file -b --mime '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx420140218-3774-17toiy6'
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]' 2>/dev/null
Command :: identify -format %m '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]'
Command :: convert '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx4[0]' -auto-orient -resize "500x500>" '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx420140218-3774-w165tz'
Command :: file -b --mime '/tmp/7f1e25f793533e2ad330a62c15c3d4b320140218-3774-b7ktx420140218-3774-w165tz'
Command :: file -b --mime-type '/tmp/ba4ac90ea15781053d509930934cc4ba20140218-3774-lxfnx3'
  MOPED: 127.0.0.1:27017 INSERT       database=parti_development collection=parts documents=[{"_id"=>BSON::ObjectId('5303dc56456c2d0ebe000000'), "name"=>"FOE", "description"=>"", "price"=>"", "clickout"=>"", "catalog_images"=>[{"_id"=>BSON::ObjectId('5303dc56456c2d0ebe010000'), "caption"=>"", "image_file_name"=>"GOPR0041.JPG", "image_content_type"=>"image/jpeg", "image_file_size"=>202999, "image_fingerprint"=>"4ad738adc42cfc8305f415a906c323a8", "image_updated_at"=>2014-02-18 22:19:02 UTC}, {"_id"=>BSON::ObjectId('5303dc56456c2d0ebe020000'), "caption"=>""}, {"_id"=>BSON::ObjectId('5303dc56456c2d0ebe030000'), "caption"=>""}], "user_images"=>[{"_id"=>BSON::ObjectId('5303dc56456c2d0ebe040000'), "caption"=>""}, {"_id"=>BSON::ObjectId('5303dc56456c2d0ebe050000'), "caption"=>""}, {"_id"=>BSON::ObjectId('5303dc56456c2d0ebe060000'), "caption"=>""}]}] flags=[]
                         COMMAND      database=parti_development command={:getlasterror=>1, :w=>1} runtime: 3.3530ms
Redirected to http://localhost:3000/parts/5303dc56456c2d0ebe000000
Completed 302 Found in 342ms (ActiveRecord: 0.0ms)


Started GET "/parts/5303dc56456c2d0ebe000000" for 127.0.0.1 at 2014-02-18 23:19:02 +0100
Processing by PartsController#show as HTML
  Parameters: {"id"=>"5303dc56456c2d0ebe000000"}
  MOPED: 127.0.0.1:27017 QUERY        database=parti_development collection=parts selector={"_id"=>BSON::ObjectId('5303dc56456c2d0ebe000000')} flags=[] limit=0 skip=0 batch_size=nil fields=nil runtime: 0.4620ms
  Rendered parts/show.html.erb within layouts/application (9.1ms)
Completed 200 OK in 16ms (Views: 13.9ms | ActiveRecord: 0.0ms)


Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/scaffolds.css?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/parts.css?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/comments.css?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/main.css?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/turbolinks.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/comments.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/parts.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-02-18 23:19:02 +0100


Started GET "/system/images/5303dc56456c2d0ebe010000/medium/GOPR0041.JPG?1392761942" for 127.0.0.1 at 2014-02-18 23:19:02 +0100

ActionController::RoutingError (No route matches [GET] "/system/images/5303dc56456c2d0ebe010000/medium/GOPR0041.JPG"):
  actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.0.2) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `block in tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:25:in `tagged'
  activesupport (4.0.2) lib/active_support/tagged_logging.rb:67:in `tagged'
  railties (4.0.2) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.0.2) 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.0.2) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  actionpack (4.0.2) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.0.2) lib/rails/engine.rb:511:in `call'
  railties (4.0.2) lib/rails/application.rb:97:in `call'
  rack (1.5.2) lib/rack/lock.rb:17:in `call'
  rack (1.5.2) lib/rack/content_length.rb:14:in `call'
  rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
  /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
  /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
  /usr/local/opt/rbenv/versions/2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

提前致谢, 斯特夫

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 mongoid paperclip mongoid4


    【解决方案1】:

    嵌入文档中的文件在保存父级时不会保存。这意味着除非您明确告诉它,否则您不能持久保存文件。在你的情况下:

    embeds_many :catalog_images, cascade_callbacks: true
    

    来自https://github.com/meskyanichi/mongoid-paperclip

    关于嵌入文档的注意事项:如果您打算保存或更新父文档,则必须将 cascade_callbacks: true 添加到您的 embeds_XXX 语句中。否则,您的数据将被更新,但不会运行回形针函数来复制/更新您的文件。

    【讨论】:

    • 这对我不起作用,但您的回答仍然让我修复了它。我不得不将“cascade_callbacks”添加到“embeds_many”行:embeds_many :catalog_images, cascade_callbacks: true
    • @Stefan - 谢谢。那是个错误。修正了我的答案:)
    【解决方案2】:

    感谢 veritas1,我得以修复它。 我必须将“cascade_callbacks = true”添加到“embeds_many”行。

    class Part
      include Mongoid::Document
      field :name
      field :description
      field :price
      field :clickout
    
      embeds_many :catalog_images, cascade_callbacks: true
    
      accepts_nested_attributes_for :catalog_images
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-25
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多