我有一个使用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
的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"
...
输出
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'
先谢谢, Steff
答案 0 :(得分:2)
保存父级时,不会保存嵌入文档中的文件。这意味着除非您明确说明,否则无法保留文件。在你的情况下:
embeds_many :catalog_images, cascade_callbacks: true
来自https://github.com/meskyanichi/mongoid-paperclip
关于嵌入式文档的注意事项:如果您计划保存或更新父文档,则必须将cascade_callbacks:true添加到embeds_XXX语句中。否则,您的数据将被更新,但回形针功能将无法运行以复制/更新您的文件。
答案 1 :(得分:0)
感谢veritas1,我能够修复它。 我不得不在“embeds_many”行添加“cascade_callbacks = true”。
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