[英]Rails 3 and carrierwave
I use Rails 3 and Carrierwave. 我使用Rails 3和Carrierwave。 I have two models:
Gallery
and GalleryPicture
: 我有两个模型:
Gallery
和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
You can find this helpful http://blog.assimov.net/post/4306595758/multi-file-upload-with-uploadify-and-carrierwave-on 您可以找到此有用的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 控制器应该与从脚手架生成的控制器相同
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.