1

I have a Rails4 Project Using Mongoid and Mongoid-Paperclip. I have ImageMagick just set up and the commands seem to work. The Model, that holds the image gets created, all the image fields get filled. The only thing that does not work is the copying of the files to the public/system/... folder (In development mode on my mac).

Here is the Code and output:

Catalog Image (embedded by Part)

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

Part (Embeds many catalog images)

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

  embeds_many :catalog_images

  accepts_nested_attributes_for :catalog_images

end

Gemfile

...
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"
...

Output

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'

Thanks in Advance, Steff

Stefan
  • 295
  • 3
  • 18

2 Answers2

3

Files in embedded documents are not saved when the parent is saved. Meaning you can't persist files unless you explicitly tell it to. In your case:

embeds_many :catalog_images, cascade_callbacks: true

From https://github.com/meskyanichi/mongoid-paperclip

Note on embedded documents: if you plan to save or update the parent document, you MUST add cascade_callbacks: true to your embeds_XXX statement. Otherwise, your data will be updated but the paperclip functions will not run to copy/update your file.

veritas1
  • 8,740
  • 6
  • 27
  • 37
  • This did not work for me, but still, your answer made me fix it. I had to add "cascade_callbacks" to the "embeds_many" line: `embeds_many :catalog_images, cascade_callbacks: true` – Stefan Feb 19 '14 at 12:41
  • @Stefan - Thanks. That was a mistake. Fixed my answer :) – veritas1 Feb 19 '14 at 12:47
0

Thanks to veritas1 I was able to fix it. I had to add "cascade_callbacks = true" to the "embeds_many" line.

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
Stefan
  • 295
  • 3
  • 18