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