Reputation: 357
I use Rails 3 and Carrierwave. I have two models: Gallery
and GalleryPicture
:
class Gallery < ActiveRecord::Base
has_many :gallery_pictures
end
class GalleryPicture < ActiveRecord::Base
belongs_to :gallery
mount_uploader :gallery_pic, GalleryUploader
end
How I can save a picture and a gallery? The following doesn't save the picture:
gallery = params[:gallery].delete(:gallery_pic)
@gallery = Gallery.new(params[:gallery])
@gallery.gallery_pictures << GalleryPicture.new(gallery)
@gallery.save
Upvotes: 4
Views: 599
Reputation: 1974
You can find this helpful http://blog.assimov.net/post/4306595758/multi-file-upload-with-uploadify-and-carrierwave-on
you can use following in your model
class Gallery < ActiveRecord::Base
has_many :gallery_pictures, :dependent => :destroy
accepts_nested_attributes_for :gallery_pictures
end
class GalleryPicture < ActiveRecord::Base
belongs_to :gallery
mount_uploader :gallery_pic, GalleryPicUploader
end
<% form_for @gallery %>
<fields>
<%= f.fields_for :gallery_pictures do |builder| %>
<% end %>
<% end %>
controller should be same as it generate from scaffold
Upvotes: 2