您好我有2个模型Products
和ProductImage
。 Product has_many ProductImages
。我正在使用paperclip gem。
正如你在我的binding.pry中看到的,我在本地有一个图像数组。如何上传这些图像并制作ProductImage。如果我继续,我会收到错误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
这是我的产品创建操作。它不是很好。将有很多重构。正如您在@form.save
之后看到的那样,您可以看到第一个代码段中显示的create_product_images
方法。
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
产品表格
<%= 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>
Rails Server As Requested
Started POST "/products" for ::1 at 2015-12-21 20:24:30 +1000
Processing by ProductsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"S71OiPhlLDY+xBkjeJIUZu5AQZrepnibC8ApZC06jRvRc0qeYDnQZBh4emheR5y64aErezFFu106rhbcnO6DLA==", "product_images"=>["20090a.jpg", "780069_black_l.jpg"], "product"=>{"category_id"=>"3", "title"=>"Test", "price"=>"345", "description"=>"test ", "size_description"=>"test", "shipping_description"=>"test", "tag_list"=>"test"}, "product_form"=>{"sizes_by_id"=>{"7"=>"32", "12"=>"4", "13"=>"", "14"=>"", "15"=>"", "16"=>"", "17"=>"", "18"=>"", "19"=>"", "20"=>""}}, "commit"=>"Create new product"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
(0.2ms) begin transaction
Category Load (0.4ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 3]]
SQL (2.1ms) INSERT INTO "products" ("title", "price", "description", "category_id", "size_description", "shipping_description", "user_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["title", "Test"], ["price", 345.0], ["description", "test "], ["category_id", 3], ["size_description", "test"], ["shipping_description", "test"], ["user_id", 1], ["created_at", "2015-12-21 10:24:31.270461"], ["updated_at", "2015-12-21 10:24:31.270461"]]
SQL (0.6ms) INSERT INTO "product_sizes" ("size_id", "quantity", "product_id") VALUES (?, ?, ?) [["size_id", 7], ["quantity", 32], ["product_id", 16]]
SQL (0.2ms) INSERT INTO "product_sizes" ("size_id", "quantity", "product_id") VALUES (?, ?, ?) [["size_id", 12], ["quantity", 4], ["product_id", 16]]
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('test'))
ActsAsTaggableOn::Tag Load (1.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 16], ["taggable_type", "Product"]]
ActsAsTaggableOn::Tagging Exists (0.4ms) SELECT 1 AS one FROM "taggings" WHERE ("taggings"."tag_id" = 10 AND "taggings"."taggable_type" = 'Product' AND "taggings"."taggable_id" = 16 AND "taggings"."context" = 'tags' AND "taggings"."tagger_id" IS NULL AND "taggings"."tagger_type" IS NULL) LIMIT 1
SQL (0.8ms) INSERT INTO "taggings" ("tag_id", "context", "taggable_id", "taggable_type", "created_at") VALUES (?, ?, ?, ?, ?) [["tag_id", 10], ["context", "tags"], ["taggable_id", 16], ["taggable_type", "Product"], ["created_at", "2015-12-21 10:24:31.315652"]]
ActsAsTaggableOn::Tag Load (0.5ms) SELECT "tags".* FROM "tags" WHERE "tags"."id" = ? LIMIT 1 [["id", 10]]
SQL (0.5ms) UPDATE "tags" SET "taggings_count" = COALESCE("taggings_count", 0) + 1 WHERE "tags"."id" = ? [["id", 10]]
(13.0ms) commit transaction
From: /Users/joseph/Documents/Safsy/Website/Safsy/Safsy/app/controllers/products_controller.rb @ line 70 ProductsController#create_product_images:
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>)>
Completed 500 Internal Server Error in 664262ms
Paperclip::AdapterRegistry::NoHandlerError (No handler found for "20090a.jpg"):
app/controllers/products_controller.rb:72:in `block in create_product_images'
app/controllers/products_controller.rb:71:in `each'
app/controllers/products_controller.rb:71:in `create_product_images'
app/controllers/products_controller.rb:51:in `create'