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.
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
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
class User < ActiveRecord::Base
has_many :products
has_many :photos,:through=>:products
end
then you can fetch User photos through
@photos =current_user.photos
or you can build a photo easily
@photo = current_user.photos.build(params[:photo])
also in your views you need to do instead of = f.file_field :photo, multiple: 'multiple'
use
= fields_for :photos do |f_i|
=f_i.file_field :image
try it .
these is the simple way for has many through association
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 these guide for more info
http://guides.rubyonrails.org/association_basics.html also you need to put
accepts_nested_attributes_for :photos
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With