I'm trying to make a create product page in rails. This includes adding multiple images. I have one model for products one for photos and for users. I'm using the paperclip gem for photo upload. But I have 2 problems.
- My file input is not allowing me to select multiple images
- When I view a product no pictures show because pictures are not being saved to database
P.S. I use HAML and I dont have a 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 } 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
Give this a try:
new product page
products controller
product model
photo model
I think the real issue here is that you want to attach multiple photo's on one model through paperclip. Paperclip can only attach 1 file per model, so I suggest you do this:
Also a good thing, you can include meta data like alt texts and stuff in the photo model now!
I also encountered same issue. I asked on stackoverflow and finally solved myself and rails cast help.
First thing to change is: You have to follow this to implement nested attributes in model side and controller end:
http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Then follow the link below, check my answer to the question it includes rails cast links that you should follow for nested forms:
You will see rails cast 2nd link dynamically separate your each filed field as on its own it fails to separate each set of fields. But there is also lack of identity uniqueness algo accuracy in rails-cast. So I enhanced to make sure the uniqueness of certain set of fields to other.
jquery render partial just one time, for second time it not render again just shows previous one
your User model not attached to photos so photos only are belongs to Product model so you need to change your User model to be
then you can fetch User photos through
or you can build a photo easily
also in your views you need to do instead of = f.file_field :photo, multiple: 'multiple'
use
try it .
these is the simple way for has many through association
you can check these guide for more info
http://guides.rubyonrails.org/association_basics.html also you need to put
inside your Product model
for a complete tutorial about nested forms you can watch these screen cast
http://railscasts.com/episodes/196-nested-model-form-revised
its not free
you can watch these free screen cast if your 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
Are you using
Resque
for background job,if yes then u need to start it usingrake resque:work QUEUE='*'
.in rails usuallyResque
is used to handle background jobs which involves mailer and picture upload.OR an example below for product.html.erb having partial for photos upload for that product with paperclip configures with Amazon S3.product.html.erb
_photos.html.erb for atleast one image mandatory
build
method only creates an object. It doesnt save the object to db. You should callobject.save
or usecreate
method instead ofbuild
in your create action