lundi 21 décembre 2015

Uploading Multiple Images In Rails Using Loop

Hello I have 2 models Products and ProductImage. Product has_many ProductImages. I am using paperclip gem.

As you can see below in my binding.pry I have an array of the images locally. How do i upload these images and make a ProductImage. If i continue i get an error No handler found for "20090a.jpg"

    69: def create_product_images
 => 70:   binding.pry
    71:   params["product_images"].each do |image|
    72:     ProductImage.create(product_image: image, product_id: @form.product.id)
    73:   end
    74: end

[1] pry(#<ProductsController>)> params["product_images"]
=> ["20090a.jpg", "780069_black_l.jpg"]
[2] pry(#<ProductsController>)> @form.product.id
=> 16
[3] pry(#<ProductsController>)>

product_image.rb

class ProductImage < ActiveRecord::Base
  belongs_to :product

  has_attached_file :product_image, styles: { large: "600x600", medium: "250x250", thumb:"100x100#"}
  validates_attachment_content_type :product_image, content_type: /\Aimage\/.*\Z/
end

Here is my Product Create Action. Its not very good. There will be alot of refactoring. As you can see after the @form.save i have the create_product_images method which is shown above in the first snippet.

  def create
    @form = ProductForm.new(
      product_image: product_params[:product_image],
      title: product_params[:title],
      price: product_params[:price],
      size_description: product_params[:size_description],
      shipping_description: product_params[:shipping_description],
      description: product_params[:description],
      tag_list: product_params[:tag_list],
      category_id: product_params[:category_id],
      sizes_by_id: product_params[:sizes_by_id],
      user: current_user
    )
    if @form.save
      create_product_images
      redirect_to @form.product
      flash[:success] = "You have created a new product"
    else
      flash[:danger] = "Your product didn't save"
      new
      render "new"
    end
  end

Product Form

<%= javascript_include_tag "custom" %>
<div class="container">
  <div class=“row”>
    <div class="col-md-6 col-md-offset-3">
      <div class="panel panel-primary">
        <div class="panel-body">
          <%= simple_form_for @product do |f| %>
            <%= file_field_tag "product_images[]", type: :file, multiple: true, label: "Upload Prouct Image" %>
            <%= f.collection_select :category_id, @categories, :id, :name, include_blank: true, prompt: "Select One Category" %>
            <% @categories.each do |category| %>
              <div class='sizes_container' id ='sizes_container_for_<%= category.id %>'>
                <% category.sizes.each do |size| %>
                  <%= label_tag "product_form[sizes_by_id][#{size.id}]", size.title %>
                  <%= text_field_tag "product_form[sizes_by_id][#{size.id}]" %>
                <% end %>
              </div>
            <% end %>
            <%= f.input :title, label:"Title"%>
            <%= f.input :price, label:"Price"%>
            <%= f.input :description,label:"Description" %>
            <%= f.input :size_description, label:"Size Details"%>
            <%= f.input :shipping_description, label:"Shipping Details"%>
            <%= f.input :tag_list,label:"Tags - Seperate tags using comma ','. 5 tags allowed per product" %>
            <%= f.button :submit, "Create new product", class: "btn-lg btn-success" %>
          <% end %>
        </div>
      </div>
    </div>
  </div>
</div>

Aucun commentaire:

Enregistrer un commentaire