I think everything is fine.. but it doesn’t work..
I uploaded whole files on github
https://github.com/iom00/action2.git
I recently updated gem. But it has same problem on rails 3.2..
Plz help me~!
portfolio model
class Portf
include Mongoid::Document
field :title, type: String
field :decs, type: String
attr_accessible :images
embeds_many :images
accepts_nested_attributes_for :images, :allow_destroy => true
end
image model
class Image
include Mongoid::Document
include Mongoid::Paperclip
field :portf_id, type: Integer
embedded_in :portf , :inverse_of => :images
has_mongoid_attached_file :file
end
portfolio controller
# GET /portfs/new
# GET /portfs/new.json
def new
@portf = Portf.new
5.times { @portf.images.build }
respond_to do |format|
format.html # new.html.erb
format.json { render json: @portf }
end
end
# GET /portfs/1/edit
def edit
@portf = Portf.find(params[:id])
5.times { @portf.images.build }
end
form
<%= form_for @portf, :html => { :multipart => true } do |f| %>
<%= f.fields_for :images do |image| %>
<% if image.object.new_record? %>
<%= image.file_field :file %>
<% end %>
<% end %>
First of all, you need to make the
images_attributesavailable for mass assignment instead ofimagestherefore you have to doYou may also want to add
:titlethere as well.Furthermore, when you have paperclip attachments in embedded documents you need to add cascade callback. From https://github.com/meskyanichi/mongoid-paperclip :
So you need to do:
You can read more on cascading callbacks here: http://mongoid.org/en/mongoid/docs/relations.html#common
Also – there is a blocker issue with mongoid-paperclip right now https://github.com/meskyanichi/mongoid-paperclip/issues/32 the pull request is there but it has not been merged yet. So you may stumble upon this error if you use it with Mongoid 3.