导入csv文件时,我收到错误:“没有将Symbol隐式转换为Integer”。添加导入照片时出错。
广告模式
require 'csv'
require 'open-uri'
class Advert < ActiveRecord::Base
has_many :photos, dependent: :destroy
private
def self.import(file)
CSV.foreach(file.path, col_sep: '||') do |row|
Advert.create!( :title => row[0],
:content => row[1],
:photos_attributes => {
:image => row[2],
}
)
end
end
end
照片模特
class Photo < ActiveRecord::Base
belongs_to :advert
validates :image, presence: true
validates_associated :advert
mount_uploader :image, ImageUploader
end
广告控制器中的
def advert_params
params.require(:advert).permit(
:title,
:content,
photos_attributes: [:id, :advert_id, :image_cache, :image, :_destroy]
)
end
CSV
my title 1||my content 1||https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
my title 2||my content 2||
my title 3||my content 3||https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
my title 4||my content 4||
my title 5||my content 5||https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png
微量
TypeError (no implicit conversion of Symbol into Integer): app/models/advert.rb:299:in block in import' app/models/advert.rb:298:in import' app/controllers/admin/adverts_controller.rb:39:in import'
UPD。从csv中删除标头。