Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image uploading isn't working using embedded documents #113

Closed
krema opened this issue Oct 1, 2013 · 10 comments
Closed

Image uploading isn't working using embedded documents #113

krema opened this issue Oct 1, 2013 · 10 comments

Comments

@krema
Copy link

krema commented Oct 1, 2013

I have problems with uploading pictures. It works only if the mount_uploader is directly set in the band model.

view:

-form_for :band, url(:bands, :create), :class => 'form-horizontal', :html => { :multipart => true } do |f|
  - f.fields_for :file do |image|
    =image.file_field :file

models:

class Band
  include Mongoid::Document
  field :name
  embeds_one :file, autobuild: true, cascade_callbacks: true, as: :photographic, class_name => 'Picture'
  accepts_nested_attributes_for :file, :allow_destroy => true
end

class Picture
  include Mongoid::Document
  embedded_in :photographic, polymorphic: true
  mount_uploader :file, Uploader
end

controller:

@band.update_attributes!(params[:band])

getting following params:

"band"=>{
    "file_attributes"=>{
        "file"=>"test.gif", 
        "id"=>"524afdbbe72db15a7300000e"
     }, 
     "name"=>"Test"
}
@krema
Copy link
Author

krema commented Oct 6, 2013

Finally I switched from Padrino to Rails and now it is working!

@rmm5t
Copy link
Member

rmm5t commented Oct 7, 2013

@croonix Thanks for the report. I have almost zero experience with Padrino. If Padrino was something that was expected to work (because it would have worked with just vanilla Carrierwave and ActiveRecord), I'm open to a pull-request and accompanying unit test.

@krema
Copy link
Author

krema commented Oct 7, 2013

ok thank you! I have the same issue if I am using third-nested embedded documents with rails3.
Double nested documents are working.

@rmm5t
Copy link
Member

rmm5t commented Oct 7, 2013

Just triple-deep embedded docs or also in combination with nested_attributes?

@krema
Copy link
Author

krema commented Oct 7, 2013

I've created a repository croonix/carrierwave_test for testing. Could you please clone this example project for me and check if it's working? I've tried to reproduce this behavior but now I'm getting the error

"undefined method `__bson_dump__' for #<ActionDispatch::Http::UploadedFile:0x007fab02245130>"

@kernelzin
Copy link

You may got here looking for the cascade_callbacks: true solution:
carrierwaveuploader/carrierwave#81

@alexkravets
Copy link

Problem happens to be in https://github.com/carrierwaveuploader/carrierwave-mongoid/blob/master/lib/carrierwave/mongoid.rb:

def find_previous_model_for_#{column}
          if self.embedded?
            ancestors       = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }

self.metadata does not exists while:

@object.assign_attributes( permit_params )
@object.save

So as a temporary fix I override find_previous_model_for_ method replacing self.metadata.key and x.first.last.metadata.key with relation name, in my case it's :location.

Does anybody know why metadata is missing?

@Goltergaul
Copy link

its __metadata in mongoid 4 i think

@Goltergaul
Copy link

see #121

@rmm5t
Copy link
Member

rmm5t commented Oct 15, 2017

Closing stale issues.

@rmm5t rmm5t closed this as completed Oct 15, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants