Search code examples
ruby-on-railsrails-4-2-1

field_for tag not showing anything rails 4


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


Solution

  • 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