Rails - paperclip - 多张照片上传未保存

6
我正在尝试在Rails中创建产品页面。这包括添加多个图像和文本字段。我有一个用于产品的模型和一个用于照片的模型。我使用paperclip gem进行照片上传。但是当我查看产品页面时,没有图片显示。照片没有保存到数据库。
附注:我使用HAML。
app/views/products/show.html.haml
  %b Name
  = @product.name
  %br

  %b Description
  = @product.description

  %br
  - @product.photos.each do |photo|
  = image_tag photo.image.url

应用程序/控制器/产品控制器

class ProductsController < ApplicationController
  before_filter :require_login
    before_filter :current_user, only: [:create, :destory]

  def new 
    @product = Product.new
    @photo = Photo.new
    5.times { @product.photos.build }
  end

  def create

  @photo = current_user.photos.build(params[:photo])
  @product = current_user.products.build(params[:product])
    if @product.save
        render "show", :notice => "Sale created!"
    else
        render "new", :notice => "Somehting went wrong!"
    end
end

  def show
    @product = Product.find(params[:id]) 
  end

app/models/photo

class Photo < ActiveRecord::Base
  attr_accessible :product_id    
  belongs_to :product
  has_attached_file :image,
    :styles => {
      :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 < ActiveRecord::Base
      attr_accessible :email, :password, :password_confirmation, :name

      attr_accessor :password
      has_many :products, dependent: :destroy
      has_many :photos,:through=>:products

app/products/new.html.haml

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

从您的Product模型中删除has_attached_file :photo,这里不需要它。 - Taimoor Changaiz
我到目前为止还不明白你为什么要再次问同样的问题...旧的那个呢?你在浪费别人的时间... - Muhammad Sannan Khalid
第三个在这里http://stackoverflow.com/questions/16807112/rails-paperclip-is-not-adding-my-image - Muhammad Sannan Khalid
5个回答

3

你在照片模型下写的是:

has_many :photos, :through => :products

这没有任何意义.. 在你的照片模型中编写
belongs_to :product

在 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 

现在就试用吧!


哎呀...这很奇怪,它们不在。 - Alain Goldman
有什么想法我应该做什么吗? - Alain Goldman

3
首先,您的表单有误,注册照片使用了fields_for,然后您使用了fields_for f.object.photos或使用Photo.new do |g|,另外在您的关系模型中,has_attached_file是has_many photos,has_attached_file Paperclip适用于要使用的模型,而不是与其他模型的关系。希望这可以帮助您,现在如果您想要一个带有多张照片的产品,我建议您使用gem cocoon,根据您的情况进行操作,https://github.com/nathanvda/cocoon

这不够表达清晰。 - Alain Goldman

1
在查看您的代码后,很明显产品根本没有任何图像属性。照片有图像属性。因此,您必须访问产品->照片->图像。请在控制器show操作中执行此操作。
@product = Product.find(id)
@photos = @product.photos

在show.html.erb中

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

对于Haml语法,我很抱歉,因为这些天一直在使用html.erb项目进行工作。如果有任何错误,请纠正Haml语法。


-1
   @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

没有起作用,最终出现了未定义方法`photos'的错误:NilClass。 - Alain Goldman
产品中有多张照片.. @product.photo 不起作用,我猜需要一个循环来显示所有的照片! - Muhammad Sannan Khalid

-1

我认为

5.times { @product.photos.build }

应该在 #new 方法中 因为 .build 方法与 @fields_for 交互


他的模型之间的关系出了问题。 - Muhammad Sannan Khalid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接