0

I started working on a rails app i want to add multiple image to my model. model/product.rb

class Product < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_many :comments , dependent: :destroy
has_many :product_images, :dependent => :destroy
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" },      default_url: "/images/:style/missing.png"
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }
end

model/product_image.rb

class ProductImage < ActiveRecord::Base
belongs_to:product
    has_attached_file :image ,:styles => {
:thumb    => ['100x100#',  :jpg, :quality => 70],
:preview  => ['480x480#',  :jpg, :quality => 70],
:large    => ['600>',      :jpg, :quality => 70],
:retina   => ['1200>',     :jpg, :quality => 30]},
:path => ':rails_root/public/system/:id.:extension',
:convert_options => {
:thumb    => '-set colorspace sRGB -strip',
:preview  => '-set colorspace sRGB -strip',
:large    => '-set colorspace sRGB -strip',
:retina   => '-set colorspace sRGB -strip -sharpen 0x0.5'
}
validates_attachment_presence :image
validates_attachment_size :image , :less_than => 10.megabytes
validates_attachment_content_type :image , :content_type =>['image/jpeg','image/jpg','image/png']
validates :image, presence: true 
end

My product form is as follows _form.html.erb

<%= form_for @product, url: products_path, :html => {:multipart => true,:class => " form-horizontal center"} do |f| %>
  <div class="field">
    <%= f.label :name,class:"control-label" %>
    <%= f.text_field :name,class:"form-control" %>
  </div>
  <div class="field">
    <%= f.label :price,class:"control-label" %>
    <%= f.number_field :price,class:"form-control"%>
  </div>
  <div class="field">
    <%= f.label :description,class:"control-label" %>
    <%= f.text_area :description,class:"form-control" %>
  </div>
  <div class="field">
    <%= f.label :reason,class:"control-label" %>
    <%= f.text_area :reason,class:"form-control" %>
  </div><br/>
  <div>
    <%= f.label :status,class:"control-label col-md-1" %>
    <div class="col-md-4">
   <%= f.select :status, [["available"], ["sold"]], {}, {class: "form-control"} %>
    </div>
    <%= f.label :category_id,class:"control-label col-md-1" %>
    <div class="col-md-4">
       <%= f.collection_select :category_id, Category.all, :id, :name,{ :prompt => "Category", :multiple => true},{class: "form-control"} %>
    </div>

    <% fields_for :product_images do |builder| %>
 

<h1>
<%= builder.label :caption, "Image Caption" %>
<%= builder.text_field :caption %>
</h1>
 
<p>
<%= builder.label :image, "Image File" %>
<%= builder.file_field :image %>
</p>
 
<% end %>
    </span>
  </div>
  <br/>
  <div class="actions">
    <%= f.submit "Submit", class: "btn btn-default btn-primary" %>
  </div><br/>
<% end %>

in my form i can see everything except labels mentioned in fileds_for tag which is used for multiple image upload. Can somebody help me with this I got my problem solved but can you help me how to use this to have multiple image upload.since i'm having for loop for images im have more labels in form and i don't want that.i want to one image selection input and i need to upload more than one image through there.help me with showing images also once product is saved

2 Answers2

0

Since the fields_for method produces output, you need the equals-sign form of the opening erb tag:

<%= fields_for :product_images do |builder| %>

This answer has some nice coverage of the variations: What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Since you're using accepts_nested_attributes_for, you probably also want to use your parent form builder when calling fields_for, unless you're handling the product_images values manually.

<%= f.fields_for :product_images do |builder| %>

For more information on Erubis, the flavor of erb that Rails uses: http://www.kuwata-lab.com/erubis

Community
  • 1
  • 1
slothbear
  • 2,016
  • 3
  • 21
  • 38
  • thanks for the help.i want to use this form for multiple image upload in case max of 3 at a time.I implemented 3.times {@product.product_images.build} in my product.new method.since its a loop its showing select image file 3 times in the form.can you help me with this im also having problem with showing images of products also – Sai Phanindhra Dec 24 '15 at 05:12
  • Each question on Stack Overflow is on just one topic. Try to get your problem down to the smallest reproducible case. Then post your problem (and code!) in a new Question. Welcome to Stack Overflow! – slothbear Dec 24 '15 at 13:18
  • A couple of requests… Could you edit the question title to change `field_for` to `fields_for`? It can be tough to find existing answers, but try a couple of combinations in the Stack Overflow search box. I found a couple of answers under `rails fields_for show` (three words from your title) but even better answers with `rails fields_for display`. Even if you don't find an answer that way, you'll often see clues for problems you didn't even know you have yet. – slothbear Dec 24 '15 at 13:48
0

You have to write

<%= f.fields_for :product_images do |builder| %>
nathanvda
  • 49,707
  • 13
  • 117
  • 139