【发布时间】:2013-05-31 16:35:54
【问题描述】:
我有一个创建产品页面,允许用户将多个图像添加到产品中。所以很自然地,我有许多图像上传字段,但是,当产品创建时,它只会拍摄一张照片并将其添加到数据库中。不是其他人。我犯了一些简单的错误,但我不知道它是什么。
感谢您的帮助!
新产品形态 (haml)
%h1
create item
= form_for @product,:url => products_path, :html => { :multipart => true } do |f|
%p
= f.label :name
= f.text_field :name
%p
= f.label :description
= f.text_field :description
%p
= fields_for :photo, :html => {:multipart => true} do |fp|
=fp.file_field :image
%p
= fields_for :photo, :html => {:multipart => true} do |fp|
=fp.file_field :image
%p.button
= f.submit
产品控制器
def new
@product = Product.new
@photo = Photo.new
end
def create
@product = current_user.products.build(params[:product])
@photo = current_user.photos.new(params[:photo])
if @product.valid? && @photo.valid?
@product.save
@photo.product_id = @product.id
@photo.save
render "show", :notice => "Sale created!"
else
render "new", :notice => "Somehting went wrong!"
end
end
【问题讨论】:
-
使用任何宝石,如回形针上传图片。并在下面查看我的答案。它可能会帮助你。
-
查看我的编辑。 “f.fields_for:照片”。你安装了回形针 gem 吗?
-
是的,我用的是宝石回形针
标签: ruby-on-rails ruby ruby-on-rails-3