Ruby on rails - paperclip not saving to database -
i'm trying make create product page in rails. includes adding multiple images. have 1 model products 1 photos , users. i'm using paperclip gem photo upload. have 2 problems.
- my file input not allowing me select multiple images
- when view product no pictures show because pictures are not being saved database
p.s. use haml , dont have photo controller.
products 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 } |f| - if @product.errors.any? .error_messages %h2 form invalid %ul - message in @product.errors.full_messages %li = message %p = f.label :name = f.text_field :name %p = fields_for :photos |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 |photo| = image_tag photo.image.url
are using resque
background job,if yes u need start using rake resque:work queue='*'
.in rails resque
used handle background jobs involves mailer , picture upload.or example below product.html.erb having partial photos upload product paperclip configures amazon s3.
product.html.erb
<%= render :partial => 'photos' %>
_photos.html.erb atleast 1 image mandatory
<% 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' : ''%> "/>
Comments
Post a Comment