Rails - paperclip - multiple photo uploads not saved

I am trying to create a product page to create in rails. This includes adding multiple images and text fields. I have one model for products and one for photos. I use a paperclip gem to upload photos. But I don’t see the pictures when I look at the product page. Photos are not saved in the database.

PS I am using 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 

application / 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 

application / 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 

application / 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 

applications / products / new.html.haml

 = form_for @product, :html => { :multipart => true } do |f| %p = fields_for :photos do |f_i| =f_i.file_field :image 
+6
source share
5 answers

First, your form is incorrect, q to register photos used by fields_for, then you use fields_for f.object.photos or use Photo.new do | g |, the other in your relationship model is incorrect. has_attached_file has has_many photos, the has_attached_file Paperclip file is suitable for use in a model that is not used in relation to another model. Hope this helps, now to have a product with several photos, I recommend you use a gemstone com, i q goes according to your case, https://github.com/nathanvda/cocoon

+3
source

You were written under the Photo Model as:

 has_many :photos, :through => :products 

It makes no sense. write in your model photo

 belongs_to :product 

while in the product model write:

 has_many :photos 

in fact, this will be a one-to-many relationship, as I understand it:

There is no need to write has_attached_file in the product model. This will be written under a type model

 has_attached_file :image 

You now have some photos for the product. Therefore, you need a loop to display these photos. for example, in your view, show something.

 <% 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 model

 belongs_to :product attr_accessible :product_id 

in your product 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 now.

+3
source

By looking at your code. Its completely straightforwardness is that the product has no image attribute at all. A photo has an image attribute.

So you need to access Products β†’ Photos β†’ Images

Do it in the show action manager

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

In show.html.erb

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

For the haml syntax, my applications, as these days are working on a project, using html.erb. The correct haml syntax is if there is any error.

+1
source

I think,

 5.times { @product.photos.build } 

should be in the #new cuz.build method; the method interacts with @fields_for

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

Source: https://habr.com/ru/post/944783/


All Articles