title: Display images from DB in a form_for RoR
body: <p>I'm trying to create an update form that would modify ( add or remove ) images related to a product. Each product has many images , each image belongs to a product.
  When a user wants to modify a product , the idea is that all the images related to the product displayed and the user decides whether to add more or remove them. My problem is I do not know how to make the images appear. I can only make it show the link.</p>
  
  <p>edit.html.erb</p>
  
  <pre><code>&lt;%- model_class = Product -%&gt;
  &lt;div class="page-header"&gt;
    &lt;h1&gt;&lt;%=t '.title', :default =&gt; [:'helpers.titles.edit', 'Edit %{model}'], :model =&gt; model_class.model_name.human.titleize %&gt;&lt;/h1&gt;
  &lt;/div&gt;
  &lt;%= render :partial =&gt; 'form' %&gt;
  </code></pre>
  
  <p>_form.html.erb</p>
  
  <pre><code>&lt;%= form_for @product, :html =&gt; { :class =&gt; "form-horizontal product" } do |f| %&gt;
  
          &lt;% if @product.errors.any? %&gt;
          &lt;div id="error_expl" class="panel panel-danger"&gt;
            &lt;div class="panel-heading"&gt;
              &lt;h3 class="panel-title"&gt;&lt;%= pluralize(@product.errors.count, "error") %&gt; prohibited this product from being saved:&lt;/h3&gt;
            &lt;/div&gt;
            &lt;div class="panel-body"&gt;
              &lt;ul&gt;
              &lt;% @product.errors.full_messages.each do |msg| %&gt;
                &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
              &lt;% end %&gt;
              &lt;/ul&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;% end %&gt;
  
        &lt;div class="control-group"&gt;
          &lt;%= f.label :name, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
            &lt;%= f.text_field :name, :class =&gt; 'form-control' %&gt;
          &lt;/div&gt;
          &lt;%= error_span(@product[:name]) %&gt;
        &lt;/div&gt;
  
  
        &lt;div class="control-group"&gt;
          &lt;%= f.label :price, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
            &lt;%= f.text_field :price, :class =&gt; 'form-control' %&gt;
          &lt;/div&gt;
          &lt;%= error_span(@product[:price]) %&gt;
        &lt;/div&gt;
        &lt;div class="control-group"&gt;
          &lt;%= f.label :stock, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
            &lt;%= f.text_field :stock, :class =&gt; 'form-control' %&gt;
          &lt;/div&gt;
          &lt;%= error_span(@product[:stock]) %&gt;
        &lt;/div&gt;
        &lt;div class="control-group"&gt;
          &lt;%= f.label :tipo, :class =&gt; 'control-label' %&gt;
          &lt;div class="controls"&gt;
            &lt;%= f.text_field :tipo, :class =&gt; 'form-control' %&gt;
          &lt;/div&gt;
          &lt;%= error_span(@product[:tipo]) %&gt;
        &lt;/div&gt;
        &lt;p&gt; &lt;hr&gt; &lt;/p&gt;
  
  
  
      &lt;%= f.fields_for :images do |builder| %&gt;
        &lt;%= render 'image_fields', f: builder %&gt;
      &lt;% end %&gt;
      &lt;%= link_to_add_fields "Add Question", f, :images %&gt;
  
  
      &lt;p&gt; &lt;hr&gt; &lt;/p&gt;
  
        &lt;%= f.submit nil, :class =&gt; 'btn btn-primary' %&gt;
        &lt;%= link_to t('.cancel', :default =&gt; t("helpers.links.cancel")),
                  products_path, :class =&gt; 'btn btn-default' %&gt;
  
      &lt;% end %&gt;
  </code></pre>
  
  <p>_image_fields.html.erb</p>
  
  <pre><code>&lt;fieldset&gt;
  
    &lt;%= f.label :link, "Imagen" %&gt;&lt;br /&gt;
    &lt;div class="col-xs-6 col-md-3"&gt;
      &lt;a href="#" class="thumbnail"&gt;
        &lt;img src="&lt;%= Here I want to introduce the link %&gt;" alt="..."&gt;
      &lt;/a&gt;
    &lt;/div&gt;
    &lt;%= f.check_box :_destroy %&gt;
    &lt;%= f.label :_destroy, "Remove Imagen" %&gt;
  &lt;/fieldset&gt;
  </code></pre>
  
  <p>products_controller.rb</p>
  
  <pre><code>class ProductsController &lt; ApplicationController
    before_action :set_product, only: [:show, :edit, :update, :destroy]
    before_action :build_image, only: [:edit]
  private
  # Use callbacks to share common setup or constraints between actions.
  def set_product
    @product = Product.find(params[:id])
  end
  
  # Never trust parameters from the scary internet, only allow the white list through.
  def product_params
    params.require(:product).permit(:name, :price, :stock, :tipo, images_attributes: [ :link ])
  end
  
  def build_image
    @images = @product.images.build
  end
  end
  </code></pre>