Rails - 回形針 - 多張照片上傳不保存 (Rails - paperclip - Multiple photo upload not saving)


問題描述

Rails ‑ 回形針 ‑ 多張照片上傳不保存 (Rails ‑ paperclip ‑ Multiple photo upload not saving)

I'm trying to make a create product page in rails. This includes adding multiple images and text fields. I have one model for products and one for photos. I'm using the paperclip gem for photo upload. But I get no picture when I view product page. Photos are not being saved to database.

P.S. I use 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

app/controllers/products_controller

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

app/models/product

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

user model

   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 

參考解法

方法 1:

First your form is wrong, q for registration of photos used fields_for, then you use fields_for f.object.photos or use Photo.new do | g |, the other in your relationship model is wrong has_attached_file is has_many photos, the has_attached_file Paperclip is proper to be used in the model to be used not in the relationship with the other model. Hope this helps, now to have a product with several photographs, I recommend you use the gem cocoon, I q goes according to your case, https://github.com/nathanvda/cocoon

方法 2:

You had written under Photo model as:

has_many :photos, :through => :products

This doesn't make any sense.. in your Photo model write

belongs_to :product

while in Product model write:

has_many :photos

actually this will be one‑to‑many relation as far i had understood:

There is no need to write has_attached_file in product model. This will be written under photo model like

has_attached_file :image

Now you have multiple photos for product. So you need loop for showing those photos. e.g., in your show view do some thing

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

_______ EDIT 2 ________

in your Product model

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

in your photo model

belongs_to :product
attr_accessible :product_id

in your products controller

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

in your 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 

try out now. !

方法 3:

After looking at your code. Its quite straight forward that Product don't have any image attribute at all. Photo have image attribute.

So you have to access Products ‑> photos ‑> images

Do this in controller show action

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

In show.html.erb

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

For haml syntax my applogies as these days working on project using html.erb. SO correct haml syntax if there is any error.

(by Alain GoldmanHuascar Ernesto MamaniMuhammad Sannan KhalidTaimoor Changaiz)

參考文件

  1. Rails ‑ paperclip ‑ Multiple photo upload not saving (CC BY‑SA 3.0/4.0)

#paperclip #ruby-on-rails-3 #ruby-on-rails #ruby






相關問題

帶有 S3 的回形針 - 未定義的方法 (Paperclip with S3 - undefined method)

添加文件上傳進度條的最簡單方法(回形針)Apache/Paperclip/Rails (Easiest way to add file upload progress bar (paperclip) Apache/Paperclip/Rails)

EngineYard:分離代碼和資產 (EngineYard : Segregating the code & assets)

在 rails/paperclip 中處理一系列圖像 (handling an array of images in rails/paperclip)

rails - không thể tạo bản ghi với tệp đã tải lên (rails - can't build a record with uploaded file)

在保存到 Rails 模型之前打開臨時文件 (Open the temporary file before it is saved in Rails model)

Rails - 回形針 - 多張照片上傳不保存 (Rails - paperclip - Multiple photo upload not saving)

回形針不為一種型號存儲替代尺寸,但為另一種型號存儲 (Paperclip is not storing alternative sizes for one model, but does for another)

無法使用 S3 圖像的 image.to_file 使用 Paperclip gem 複製圖像 (Can't copy images with Paperclip gem using image.to_file for S3 images)

Paperclip.interpolates 隨機不返回值 (Paperclip.interpolates randomly not returning a value)

未初始化的常量 AWS::S3::NoSuchBucket (uninitialized constant AWS::S3::NoSuchBucket)

來自基類的 Rails STI 條件子類化 (Rails STI conditional sub-classing from base class)







留言討論