2014-02-19 70 views
0

我有一個使用Mongoid和Mongoid-Paperclip的Rails4項目。 我有ImageMagick設置和命令似乎工作。 保存圖像的模型被創建,所有的圖像字段被填充。 唯一不起作用的是將文件複製到public/system/...文件夾(在我的mac上的開發模式中)。Rails4 Mongoid回形針不會上傳文件,但其他所有內容

這裏是代碼和輸出:

目錄圖像(嵌入由部分)

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

回答

2

文件中嵌入文檔當父保存不保存。意思是你不能堅持文件,除非你明確告訴它。你的情況:

embeds_many :catalog_images, cascade_callbacks: true 

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

注意嵌入式文件:如果你打算保存或更新父文檔,您必須添加cascade_callbacks:忠於你embeds_XXX聲明。否則,您的數據將被更新,但回形針功能將不會運行以複製/更新您的文件。

+0

這並沒有爲我工作,但仍然,你的回答讓我修復它。我不得不在「embeds_many」行添加「cascade_callbacks」: 'embeds_many:catalog_images,cascade_callbacks:true' – Stefan

+0

@Stefan - 謝謝。那是個錯誤。修正了我的答案:) – veritas1

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