lundi 21 décembre 2015

PaperClip No handler found Error

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

When i submit the Product Form i get this error.

No handler found for ["0", {"product_image"=>#<ActionDispatch::Http::UploadedFile:0x007f8cb78b29c8 @tempfile=#<Tempfile:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151221-46388-dqxfk7.jpg>, @original_filename="780069_black_l.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n">}]

  def create_product_images
    params["product"]["product_images_attributes"].each do |image|
      ProductImage.create(product_image: image, product_id: @form.product.id)
    end
  end

Anyone know how to fix this? Im trying to create a ProductImage. So when i go product.product_images i have an array of all the images for the product.

Here is the 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| %>
            <%= f.fields_for :product_images do |product_image| %>
              <% if product_image.object.new_record? %>
                <%= product_image.file_field(:product_image) %>
              <% else %>
                <%= image_tag(product_image.url(:thumb)) %>
                <%= product_image.hidden_field :_destroy %>
                <%= product_image.link_to_remove "X" %>
              <% end %>
            <% end %>
            <%= 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>

This is what the params look like

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

[1] pry(#<ProductsController>)> params["product"]["product_images_attributes"]
=> {"0"=>
  {"product_image"=>
    #<ActionDispatch::Http::UploadedFile:0x007f8cacd89b28
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][0][product_image]\"; filename=\"780069_black_l.jpg\"\r\nContent-Type: image/jpeg\r\n",
     @original_filename="780069_black_l.jpg",
     @tempfile=#<File:/var/folders/yx/znmx6qfj0c507bvkym6lvhxh0000gn/T/RackMultipart20151221-46388-d4pub9.jpg>>},
 "1"=>
  {"product_image"=>
    #<ActionDispatch::Http::UploadedFile:0x007f8cacd897b8
     @content_type="image/jpeg",
     @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1][product_image]\"; filename=\"20090a.jpg\"\r\nContent-Type: image/jpeg\r\n",
     @original_filename="20090a.

Aucun commentaire:

Enregistrer un commentaire