2011-06-17 97 views
4

我使用Rails 3和Carrierwave。我有兩個型號:GalleryGalleryPictureRails 3和Carrierwave

class Gallery < ActiveRecord::Base 
    has_many :gallery_pictures 
end 

class GalleryPicture < ActiveRecord::Base 
    belongs_to :gallery 
    mount_uploader :gallery_pic, GalleryUploader 
end 

我如何保存圖片和畫廊?下列不保存圖片:

gallery = params[:gallery].delete(:gallery_pic) 
@gallery = Gallery.new(params[:gallery]) 
@gallery.gallery_pictures << GalleryPicture.new(gallery) 
@gallery.save 

回答

2

可以有所幫助 http://blog.assimov.net/post/4306595758/multi-file-upload-with-uploadify-and-carrierwave-on

你可以使用你的模型如下

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 %> 

控制器應該是相同的,因爲它從支架產生