Rails - paperclip - Multiple photo upload not saving

江枫思渺然 提交于 2019-12-05 06:37:29

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

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. !

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.

I think

5.times { @product.photos.build }

should be in #new method cuz .build method interact with @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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!