我正在使用Paperclip,我已经为我的一个模型添加了多个文件上传。除非我尝试使用新文件进行更新,否则一切正常。它抛出了这个错误:
can't convert String into Integer
app/controllers/listings_controller.rb:86:in `block in update'
app/controllers/listings_controller.rb:85:in `update'
如何解决此问题,以便我可以使用新文件进行更新?提前谢谢。
asset.rb
class Asset < ActiveRecord::Base
belongs_to :member
belongs_to :listing
attr_accessible :asset
has_attached_file :asset, styles: { large: "700x700>", thumb: "100x100#" }
validates_attachment_size :asset, :less_than_or_equal_to=>10.megabyte
validates_attachment_content_type :asset, :content_type=>['image/jpeg', 'image/jpg', 'image/png', 'image/gif']
end
listing.rb
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets, :allow_destroy => true
attr_accessible :assets_attributes
目录/ _edit_form.html.erb
<%= simple_form_for(@listing, :html => { class: 'form-horizontal ', :multipart => true }) do |f| %>
<% if @listing.errors.any? %>
<%= f.error_notification %>
<div>
<%= file_field_tag('listing_assets_asset', multiple: true, name: "listing[assets_attributes][][asset]", id: 'file-upload3', class: '') %>
</div>
<% end %>
listings_controller.rb
before_filter :authenticate_member!, only: [:new, :create, :edit, :update, :destroy]
before_filter :find_member
before_filter :find_listing, only: [:edit, :update, :destroy]
def new
@listing = Listing.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @listing }
end
end
# GET /listings/1/edit
def edit
end
# POST /listings
# POST /listings.json
def create
@listing = current_member.listings.new(params[:listing])
respond_to do |format|
if @listing.save
current_member.create_activity(@listing, 'created')
format.html { redirect_to @listing }
format.json { render json: @listing, status: :created, location: @listing }
else
format.html { render action: "new" }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
# PUT /listings/1
# PUT /listings/1.json
def update
respond_to do |format|
if @listing.update_attributes(params[:listing])
format.html { redirect_to @listing }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @listing.errors, status: :unprocessable_entity }
end
end
end
private
def find_member
@member = Member.find_by_user_name(params[:user_name])
end
def find_listing
@listing = current_member.listings.find(params[:id])
end
参数
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"ilfHooJlfqXqEoHyB6GD6uoXG33Vqs6eliIec6tToXo=",
"listing"=>{"title"=>"Testing inputs",
"assets_attributes"=>{"asset"=>[#<ActionDispatch::Http::UploadedFile:0x777a788 @original_filename="0002k2qr.jpeg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"listing[assets_attributes][asset][]\"; filename=\"0002k2qr.jpeg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/owner/AppData/Local/Temp/RackMultipart20150320-7872-1hju1df>>,
#<ActionDispatch::Http::UploadedFile:0x777a1a0 @original_filename="2Pac FtNotorious BIG NotoriousBIGandTupacShakur.jpg",
@content_type="image/jpeg",
@headers="Content-Disposition: form-data; name=\"listing[assets_attributes][asset][]\"; filename=\"2Pac+FtNotorious+BIG+NotoriousBIGandTupacShakur.jpg\"\r\nContent-Type: image/jpeg\r\n",
@tempfile=#<File:C:/Users/owner/AppData/Local/Temp/RackMultipart20150320-7872-re8hr6>>],
"0"=>{"_destroy"=>"0",
"id"=>"29"},
"1"=>{"_destroy"=>"0",
"id"=>"30"},
"2"=>{"_destroy"=>"0",
"id"=>"31"},
"3"=>{"_destroy"=>"0",
"id"=>"32"},
"4"=>{"_destroy"=>"0",
"id"=>"33"},
"5"=>{"_destroy"=>"0",
"id"=>"35"}},
"category"=>"Music",
"description"=>""},
"commit"=>"Update Listing",
"id"=>"16-testing-inputs"}
development.log
Started PUT "/market/listings/16-testing-inputs" for 127.0.0.1 at 2015-03-20 22:11:58 -0700
Processing by ListingsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ilfHooJlfqXqEoHyB6GD6uoXG33Vqs6eliIec6tToXo=", "listing"=>{"title"=>"Testing inputs", "assets_attributes"=>{"asset"=>[#<ActionDispatch::Http::UploadedFile:0x777a788 @original_filename="0002k2qr.jpeg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[assets_attributes][asset][]\"; filename=\"0002k2qr.jpeg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/owner/AppData/Local/Temp/RackMultipart20150320-7872-1hju1df>>, #<ActionDispatch::Http::UploadedFile:0x777a1a0 @original_filename="2Pac FtNotorious BIG NotoriousBIGandTupacShakur.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"listing[assets_attributes][asset][]\"; filename=\"2Pac+FtNotorious+BIG+NotoriousBIGandTupacShakur.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:C:/Users/owner/AppData/Local/Temp/RackMultipart20150320-7872-re8hr6>>], "0"=>{"_destroy"=>"0", "id"=>"29"}, "1"=>{"_destroy"=>"0", "id"=>"30"}, "2"=>{"_destroy"=>"0", "id"=>"31"}, "3"=>{"_destroy"=>"0", "id"=>"32"}, "4"=>{"_destroy"=>"0", "id"=>"33"}, "5"=>{"_destroy"=>"0", "id"=>"35"}}, "description"=>""}, "commit"=>"Update Listing", "id"=>"16-testing-inputs"}
[1m[35mMember Load (0.0ms)[0m SELECT "members".* FROM "members" WHERE "members"."id" = 1 LIMIT 1
[1m[36mMember Load (1.0ms)[0m [1mSELECT "members".* FROM "members" WHERE "members"."user_name" IS NULL LIMIT 1[0m
[1m[35mListing Load (0.0ms)[0m SELECT "listings".* FROM "listings" WHERE "listings"."member_id" = 1 AND "listings"."id" = ? LIMIT 1 [["id", "16-testing-inputs"]]
[1m[36m (0.0ms)[0m [1mbegin transaction[0m
[1m[36m (0.0ms)[0m [1mrollback transaction[0m
Completed 500 Internal Server Error in 51ms
TypeError (can't convert String into Integer):
app/controllers/listings_controller.rb:86:in `block in update'
app/controllers/listings_controller.rb:85:in `update'
答案 0 :(得分:0)
我通过查看这篇文章找到了解决问题的方法:http://www.railscook.com/recipes/multiple-files-upload-with-nested-resource-using-paperclip-in-rails/
我需要改变处理多个上传的方式。
将我的输入更改为:
<%= file_field_tag "assets[]", type: :file, multiple: true, id: 'file-upload3' %>
并将以下内容添加到我的控制器中的创建和更新操作中:
if params[:assets]
params[:assets].each { |asset|
@listing.assets.create(asset: asset)
}
end
做这些事情解决了我的问题。