我使用Rails 3和Carrierwave.我有两个模型:Gallery和GalleryPicture:
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
最佳答案 你会发现这很有用
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 %>
控制器应该与脚手架生成的控制器相同