Ruby on rails 使用Loop在Rails中上载多个图像
您好,我有两款产品和ProductImage。产品有很多产品图片。我用的是回形针 正如你们在我的binding.pry中看到的,我在本地有一个图像数组。如何上传这些图片并制作产品图片。如果我继续,我会得到一个错误,没有找到20090a.jpg的处理程序 产品形式Ruby on rails 使用Loop在Rails中上载多个图像,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 4,您好,我有两款产品和ProductImage。产品有很多产品图片。我用的是回形针 正如你们在我的binding.pry中看到的,我在本地有一个图像数组。如何上传这些图片并制作产品图片。如果我继续,我会得到一个错误,没有找到20090a.jpg的处理程序 产品形式 <%= javascript_include_tag "custom" %> <div class="container"> <div class=“row”> <div class
<%= 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服务器
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'
您的参数没有任何图像文件,只有一个带有文件名的字符串。你能分享一下相关的表格吗?同时为你的应用程序添加正确的标签,Rails 3或Rails 4。我添加了产品表单。谢谢。将文本\字段\标记更改为“这是”标记以用于不同的目的。您还可以添加Rails服务器日志文件和相关请求。根据日志文件,服务器请求中没有任何图像。@我知道。这就是我写问题的原因。
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>
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'