【发布时间】:2014-07-25 18:43:06
【问题描述】:
我正在尝试上传包含图像 URL 作为字段之一的 CSV 文件。我正在使用回形针在我的 Rails 4 应用程序中处理图像。这是我模型中的导入代码。你能告诉我下面的语法有什么问题吗——特别是 URI 解析代码?如果我从 CSV 文件中删除图像字段,则所有内容都会正确上传。
我希望 rails 从 url 获取 jpg 文件并将其保存在我们的服务器上。这是一个电子商务应用,所以我正在尝试为每个产品加载产品图片。
require 'csv'
require 'open-uri'
def self.import(file)
CSV.foreach(file.path, headers: true) do |row|
listing_hash = {:name => row['Name'], :description => row['Description'],
:price => row['Price'], :image => URI.parse.row['Image'] }
listing = Listing.where(id: listing_hash["id"])
if listing.count == 1
listing.first.update_attributes(listing_hash)
else
Listing.create!(listing_hash)
end # end if !product.nil?
end # end CSV.foreach
end # end self.import(file)
【问题讨论】:
-
您是否收到
ArgumentError: wrong number of arguments (0 for 1)错误?
标签: ruby-on-rails csv paperclip