我使用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
答案 0 :(得分: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 %>
控制器应与从脚手架生成的控制器相同