Ruby on rails - clip is not stored in database

I am trying to create a product page to create in rails. This includes adding multiple images. I have one model for products, one for photos and for users. I use a paperclip gem to upload photos. But I have 2 problems.

  • My input file does not allow me to select multiple images
  • When I browse the product, the images are not displayed because the images are not saved in the database

PS I use HAML and I do not have a photo controller.

Product controller

class ProductsController < ApplicationController before_filter :current_user, only: [:create, :destory] before_filter :correct_user, only: :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 

create product page

 = 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.label :name = f.text_field :name %p = fields_for :photos do |f_i| =f_i.file_field :image %p.button = f.submit 

product model

 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 

photo model

 class Photo < ActiveRecord::Base attr_accessible :product_id belongs_to :product has_attached_file :image, :styles => { :thumb=> "100x100#", :small => "300x300>", :large => "600x600>" } 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 

show product page

  %b seller = @product.user.name %br - @product.photos.each do |photo| = image_tag photo.image.url 
+6
source share
6 answers

Do you Resque for the background job, if so, you need to run it with rake resque:work QUEUE='*' rails usually Resque used to handle background jobs related to downloading the mail program and image. An example is below for product.html.erb with partial loading of photos for this product with paperclip setup using Amazon S3.

product.html.erb

 <%= render :partial => 'photos' %> 

_photos.html.erb for at least one image required

  <% if @product.photos[0].nil? %> <a href="javascript:void(0);" class="add-photos" > <img src="/assets/default/product-add-photos.png" alt="Add product photos"/> </a> <% end %> <img src="<%= (@product.product_photos[0].nil? ? "" : @product.photos[0].image.url(:small)) %>" id="photos_1" class="product-photos-src <% =@product.photos [0].nil? ? 'dontdisplay' : ''%> "/> 
+1
source

your user model is not attached to photos, so the photos belong only to the product model, so you need to change your user model

  class User < ActiveRecord::Base has_many :products has_many :photos,:through=>:products end 

then you can get user photos through

  @photos =current_user.photos 

or you can easily create a photo.

 @photo = current_user.photos.build(params[:photo]) 

also in your views that you need to do, not = f.file_field: photo, multiple: 'multiple'

use

 = fields_for :photos do |f_i| =f_i.file_field :image 

try it.

this is an easy way because it has many cross-associations

  class Document < ActiveRecord::Base has_many :sections has_many :paragraphs, :through => :sections end class Section < ActiveRecord::Base belongs_to :document has_many :paragraphs end class Paragraph < ActiveRecord::Base belongs_to :section end 

You can check this guide for more information.

http://guides.rubyonrails.org/association_basics.html also you need to put

  accepts_nested_attributes_for :photos 

inside your product model

for a complete guide on nested forms you can watch these screens

http://railscasts.com/episodes/196-nested-model-form-revised

its not free

you can watch these free screen shots if you are not subscribed to railscasts.com

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

+3
source

Try:

product page

 = form_for @product, :html => {:multipart => true} do |f| %p = f.label :description = f.text_field :description = f.fields_for :photo do |fp| = fp.file_field :image = fp.check_box :_destroy = fp.label :_destroy, "Remove Image" %p.button = f.submit 

product controller

 def new @product = Product.new @product.photos.build end def create @product = current_user.products.create(params[:product]) # or # @product = current_user.products.build(params[:product]) # @product.save end 

product model

 class Product < ActiveRecord::Base attr_accessible :description, :name, :photo accepts_nested_attributes_for :photo, :reject_if => lambda { |p| p[:image].nil? }, :allow_destroy => true belongs_to :user has_many :photos, dependent: :destroy validates :user_id, presence: true validates :photo, presence: true end 

photo model

 class Photo < ActiveRecord::Base attr_accessible :image belongs_to :product validates_attachment :image, presence: true, content_type: { content_type: ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'] }, size: { less_than: 5.megabytes } has_attached_file :image, styles: { medium: "320x240>"} end 
+2
source
  • If you use a nested form, you do not need to create each object separately. Creating a product will create an attached photo.
  • build method creates only the object. It does not save the object in db. You must call object.save or use the create method instead of build in your create action
+1
source

I think the real problem is that you want to attach multiple photos to the same model using paperclip. Paperclip can only attach 1 file per model, so I suggest you do this:

 1. create model Photo with paperclip migrations + has_attached_file 2. Product has_many :photos 3. (optional) make a function to return all image urls, otherwise just call the method in the view class Product def get_pics photos.collect{|p| p.image.url} end end 

Also good, you can now include metadata such as alternate texts and more in the photo model!

 <% @product.photos.each do |photo| %> <%= image_tag photo.image.url, :alt => photo.alt_text %> <% end %> 
0
source

I also ran into the same problem. I asked about stackoverflow and finally decided it myself and the rails threw help.

The first thing to change: You must follow this to implement the nested attributes on the model side and at the end of the controller:

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Then follow the link below, check out my answer to the question that it includes links to rails that you should follow nested forms:

You will see that the cast rails of the 2nd line dynamically separate your field for each field, since it cannot separate each set of fields. But there is also a lack of accuracy of identity identity on rails. Therefore, I was expanded to make sure the uniqueness of a certain set of fields for others.

jquery draws partial only once, the second time it doesn't display again, just shows the previous one

0
source

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


All Articles