samedi 18 juillet 2015

Display images from DB in a form_for RoR

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.

edit.html.erb

<%- model_class = Product -%>
<div class="page-header">
  <h1><%=t '.title', :default => [:'helpers.titles.edit', 'Edit %{model}'], :model => model_class.model_name.human.titleize %></h1>
</div>
<%= render :partial => 'form' %>

_form.html.erb

<%= form_for @product, :html => { :class => "form-horizontal product" } do |f| %>

        <% if @product.errors.any? %>
        <div id="error_expl" class="panel panel-danger">
          <div class="panel-heading">
            <h3 class="panel-title"><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h3>
          </div>
          <div class="panel-body">
            <ul>
            <% @product.errors.full_messages.each do |msg| %>
              <li><%= msg %></li>
            <% end %>
            </ul>
          </div>
        </div>
      <% end %>

      <div class="control-group">
        <%= f.label :name, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :name, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:name]) %>
      </div>


      <div class="control-group">
        <%= f.label :price, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :price, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:price]) %>
      </div>
      <div class="control-group">
        <%= f.label :stock, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :stock, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:stock]) %>
      </div>
      <div class="control-group">
        <%= f.label :tipo, :class => 'control-label' %>
        <div class="controls">
          <%= f.text_field :tipo, :class => 'form-control' %>
        </div>
        <%= error_span(@product[:tipo]) %>
      </div>
      <p> <hr> </p>



    <%= f.fields_for :images do |builder| %>
      <%= render 'image_fields', f: builder %>
    <% end %>
    <%= link_to_add_fields "Add Question", f, :images %>


    <p> <hr> </p>

      <%= f.submit nil, :class => 'btn btn-primary' %>
      <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
                products_path, :class => 'btn btn-default' %>

    <% end %>

_image_fields.html.erb

<fieldset>

  <%= f.label :link, "Imagen" %><br />
  <div class="col-xs-6 col-md-3">
    <a href="#" class="thumbnail">
      <img src="<%= Here I want to introduce the link %>" alt="...">
    </a>
  </div>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove Imagen" %>
</fieldset>

products_controller.rb

class ProductsController < 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

Aucun commentaire:

Enregistrer un commentaire