1

I have a create product page that allows users to add multiple images to a product. So naturally I have a number of image upload fields however, when the product is created it will only take ONE PHOTO and add it to the database. Not the others. I am making some simple mistake but I have no idea what it is.

Thanks for the help !

new product form (haml)

%h1 
  create item
= form_for @product,:url => products_path, :html => { :multipart => true } do |f|
  %p
    = f.label :name
    = f.text_field :name
  %p
    = f.label :description
    = f.text_field :description
  %p
    = fields_for :photo, :html => {:multipart => true} do |fp|
      =fp.file_field :image  
  %p
    = fields_for :photo, :html => {:multipart => true} do |fp|
      =fp.file_field :image  

  %p.button
    = f.submit

products controller

  def new 
    @product = Product.new
    @photo = Photo.new
  end


  def create
  @product = current_user.products.build(params[:product])
  @photo = current_user.photos.new(params[:photo])

    if @product.valid? && @photo.valid?
      @product.save
      @photo.product_id = @product.id
      @photo.save
      render "show", :notice => "Sale created!"
    else
      render "new", :notice => "Somehting went wrong!"
    end
end
3
  • Use any gems like paperclip to upload images. And See my answer below. It may help you. Commented Jun 5, 2013 at 5:48
  • see my edit. "f.fields_for :photos". Have you installed paperclip gem? Commented Jun 5, 2013 at 6:32
  • yeah i use gem paperclip Commented Jun 5, 2013 at 6:57

4 Answers 4

1

You are trying to implement nested attributes form. So change fields_for part to

%p
  = f.fields_for :photos do |fp|
    =fp.file_field :image

Then change your new method in controller

def new 
  @product = Product.new
  3.times{ @product.photos.build }
end


def create
  @product = current_user.products.new(params[:product])
  if @product.valid?
    @product.save
    render "show", :notice => "Sale created!"
  else
    render "new", :notice => "Something went wrong!"
  end
end

Your Models should be related like the following

class Product
  has_many :photos
  accepts_nested_attributes_for :photos, allow_destroy: true
end

class Photo
  attr_accessible :image
  belongs_to :product
end

If you want to implement a "Add More" button for photos, You can use Gem nested_form. Its very nice one.

Sign up to request clarification or add additional context in comments.

2 Comments

I looked at this again and implemented it and it gives me 3 image upload fields even though i only put one in the html so i guess your answer is working so far. but i get this error Can't mass-assign protected attributes: photos_attributes.. which is something i never got before. Any ideas?
See my change in Photos Model. We have to specify the attr_accessible for the particular field.
1

Here you have Used two fields_for tag which generate a Input file element with same name.

so always you get only one file save as they bot element have same name and parameters in rails are passed by using input element name. so here you need to use file_field_tag and catch the parameters into a controller for saving into a database.

1 Comment

How would I go about that.
1

This can achived through nested attributes. Please read this Rails doc.

Also see:

Comments

1

You can use the Gem carrierwave for the server and you can use plupload http://www.plupload.com/ for the front it is very simple

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.