1

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...

  1. Find some way to store this photo in a session (is this possible in Rails?)
  2. 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
Alain Goldman
  • 2,786
  • 4
  • 39
  • 70

3 Answers3

1

depending on how the form behavior is, it might be better to simply using client side image preview without the need to use ajax (or sending request) at all..

https://github.com/blueimp/JavaScript-Load-Image is a simple to use js plugin but it's not compatible with older browser

to support browser that didn't support html5 you can use https://github.com/mailru/FileAPI which offer flash based fallback

Roy
  • 556
  • 3
  • 11
  • Hey Roy, you always have such good answers! I downloaded the javascript plugin and it doesn't work. I then set it up in my own app and I couldn't get it to work either :/ – Alain Goldman Jul 01 '13 at 17:59
  • which plugin did you use? loadimage should be easy to use, just follow example in the readme.. – Roy Jul 02 '13 at 07:24
0

Don't hold the image in memory at all. You can read it from disk easily as fast as you can send it across the wire so having it in memory is no advantage unless it's a thumbnail that is being hit repeatedly.

There are other caching mechanisms besides holding it in Rails' memory. Let Rails use its memory to run its processes.

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
  • oh sweet caching mechanisms like what – Alain Goldman Jul 01 '13 at 00:13
  • memcached, or any RAM based database would be a starting point. Check out http://stackoverflow.com/q/10558465/128421, http://stackoverflow.com/q/7888880/128421 and http://stackoverflow.com/q/4208912/128421 and follow the linked and related links on the right. – the Tin Man Jul 01 '13 at 00:20
0

I accomplished this by relaxing the constraint that they have a product and adding an enabled flag.

  1. Upload the image using AJAX, and create the photo model. product_id is null, enabled is false.
  2. Display the thumbnail. Your form should include the photo_id, enabled (set to true) and any other relevant fields
  3. Submit the product create form. The photo model gets associated with the product and enabled get sets to true

The enabled flag isn't very interesting for the create case, but you'll want it for updates and creates that are cancelled. You can write a scheduled job that deletes any photos not associated with product.

John Naegle
  • 7,678
  • 3
  • 33
  • 47
  • Ok! Sweet but can you give me any pointers on what i should do in the new product page and on the product controller? (i added them in the edit) – Alain Goldman Jul 01 '13 at 02:53
  • I would make a new controller, just for photos. It could have a single action, create, that gets the AJAX post when you create a new photo. – John Naegle Jul 01 '13 at 13:17