I have a product page that allows users to add some text and a photo. Product is a model and so is photo. Naturally, each photo REQUIRES a product. So I want the photo to show a thumbnail before the product is submitted.
Should I...
- Find some way to store this photo in a session (is this possible in Rails?)
- Or, make another model to store this photo temporarily and load it instantly with AJAX
P.S. I'm using gem 'paperclip'
Edit: new.html.haml
= form_for @product,:url => products_path, :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
- if @photo.errors.any?
.error_messages
%h2 Image is invalid
%ul
- for message in @photo.errors.full_messages
%li
= message
%p
= f.label :name
= f.text_field :name
%p
= f.label :description
= f.text_field :description
%p
= f.fields_for :photos do |fp|
=fp.file_field :image
%br
%p.button
= f.submit
products controller
def new
@product = Product.new
@photo = Photo.new
# 4.times{ @product.photos.build }
@product.photos.build
end
def create
@product = current_user.products.new(params[:product])
@photo = current_user.photos.new(params[:photo])
if @product.valid?
@product.save
@photo.product_id = @product.id
@photo.save
render "show", :notice => "Sale created!"
else
# 4.times{ @product.photos.build }
@product.photos.build
render "new", :notice => "Somehting went wrong!"
end
end