Rails – paperclip – 多张照片上传不保存

我正在尝试在rails中创建一个创建产品页面。 这包括添加多个图像和文本字段。 我有一个产品型号和一个照片。 我正在使用paperclip gem进行照片上传。 但是当我查看产品页面时,我没有得到任何图片。 照片未保存到数据库。

PS我使用HAML。

应用程序/视图/产品/ show.html.haml

%b Name = @product.name %br %b Description = @product.description %br - @product.photos.each do |photo| = image_tag photo.image.url 

应用程序/控制器/ products_controller

 class ProductsController  "Sale created!" else render "new", :notice => "Somehting went wrong!" end end def show @product = Product.find(params[:id]) end 

应用程序/模型/摄

 class Photo  { :thumb=> "100x100#", :small => "300x300>", :large => "600x600>" } end 

应用程序/模型/产品

 class Product < ActiveRecord::Base attr_accessible :description, :name, :price, :condition, :ship_method, :ship_price, :quantity, :photo has_many :photos, dependent: :destroy accepts_nested_attributes_for :photos belongs_to :user end 

用户模型

  class User :products 

应用程序/产品/ new.html.haml

 = form_for @product, :html => { :multipart => true } do |f| %p = fields_for :photos do |f_i| =f_i.file_field :image 

首先你的表单是错的,q用于注册使用fields_for的照片,然后你使用fields_for f.object.photos或使用Photo.new do | g |,你的关系模型中的另一个是错误的has_attached_file是has_many照片,has_attached_file Paperclip适合在模型中使用而不是与其他模型的关系。 希望这有帮助,现在有一个产品有几张照片,我建议你使用gem茧,我问你根据你的情况, https://github.com/nathanvda/cocoon

你曾在Photo模型下写过:

 has_many :photos, :through => :products 

这没有任何意义..在你的Photo模型中写

 belongs_to :product 

在产品型号中写道:

 has_many :photos 

实际上这是我所理解的一对多关系:

无需在产品模型中编写has_attached_file。 这将写在照片模型下

 has_attached_file :image 

现在您有多张产品照片。 所以你需要循环来显示这些照片。 例如,在你的节目视图中做一些事情

 <% unless @product.photos.blank? %> <% @product.photos.each do |photo| %> <%= image_tag photo.image.url %> <% end %> <% end %> 

__ _ __ _ _编辑2 _ __ _ __ _ _

在您的产品型号中

 has_many :photos, :dependent => :destroy accepts_nested_attributes_for :photos, :allow_destroy => true attr_accessible :photos_attributes 

在你的照片模型中

 belongs_to :product attr_accessible :product_id 

在您的产品控制器中

 def new @product = Product.new 5.times { @product.photos.build } end def create @product = Product.new(params[:product]) @product.user_id = current_user.id if @product.save render "show", :notice => "Sale created!" else render "new", :notice => "Somehting went wrong!" end end 

在你的new.html.haml中

 = form_for @product, :html => { :multipart => true } do |f| - if @product.errors.any? .error_messages %h2 Form is invalid %ul - for message in @product.errors.full_messages %li = message %p = f.fields_for :photos do |f_i| =f_i.file_field :image 

现在试试吧。 !

看了你的代码。 很简单,Product根本没有任何图像属性。 照片有图像属性。

所以你必须访问产品 – >照片 – >图像

在控制器show action中执行此操作

 @product = Product.find(id) @photos = @product.photos 

在show.html.erb中

 -@photos.each do |photo| = image_tag photo.image.url -end 

对于haml语法我的applogies现在使用html.erb处理项目。 如果有任何错误,请更正haml语法。

我认为

 5.times { @product.photos.build } 

应该在#new方法中,cuz .build方法与@fields_for进行交互

  @product.image requires image to be the attribute of product model means image fields should be in products table. @product.image should be @product.photo.image.url