Rails 3 和载波

发布于 2024-11-15 19:54:00 字数 526 浏览 1 评论 0原文

我使用 Rails 3 和 Carrierwave。我有两个模型:GalleryGalleryPicture

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来日方长 2024-11-22 19:54:00

您会发现这很有帮助
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 %>

控制器中使用以下内容,应该与从脚手架生成的相同

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文