From active_admin (administrator screen)
using Carrierwave
I was posting images one by one.
You need to delete the data in the database once
Therefore,
Ke: "Is it going to be posted one by one again? I'm tired, is there any good way?"
It started from. Lol
The file changed this time is as follows
lib/tasks/import_csv.rake
db/csv_data/text_data.csv
It was a lot of work. Lol
Let's go
Originally here
lib/tasks/import_csv.rake
desc "Task to import TEXT data"
task movie_data: :environment do
list = Import.csv_data(path: "db/csv_data/text_data.csv")
Text.create!(list)
end
The image cannot be uploaded at this rate.
Then use the open
method to instruct it to open the image file.
lib/tasks/import_csv.rake
desc "Task to import TEXT data"
task text: :environment do
list = Import.csv_data(path: "db/csv_data/text_data.csv")
* File.Rails with join.root+"db/csv_data/text_data.csv"To combine.
path = File.join Rails.root, "db/csv_data/text_data.csv"
CSV.foreach(path, headers: true) do |row|
list << {
image: File.open("#{Rails.root}/#{row['image']}")
}
end
puts "Start import process"
Text.create!(list)
puts "Import was successful"
end
Since CSV.foreach
is used to process lines one by one, it can be used without worrying about memory usage even when handling CSV files with many lines.
After finishing this description, the next step is to edit the CSV file!
db/csv_data/text_data.csv
image,genre,title,content
"db/fixtures/Slide 2.jpeg ",Basic,Mac shortcut keys,"If you are not familiar with your computer, let's start by using shortcut keys. If you can't use the shortcut keys listed here, the learning efficiency of programming will not improve at all.
Describe db/fixtures/slide 2.jpeg
and the path of the file.
In my case, I still have more data, so
Give the path of the file in the same way ...
** This was sober ** ww
After writing, let's import the task next!
Terminal
rake -T
.
.
.
.
rake import_csv:text #Task to import TEXT data
Check the command
Copy and execute rake import_csv: text
Terminal
rake import_csv:text
.
.
.
.
Start import process
rake aborted!
ActiveRecord::RecordInvalid:Validation failed:Title cannot be left blank,Genre cannot be blank,Content cannot be blank
I thought the code was a little strange, but it seems like it's still lol
When I read the error statement, it seems that there is no data in other columns,
Let's look at the code again.
lib/tasks/import_csv.rake
desc "Task to import TEXT data"
task text: :environment do
list = Import.csv_data(path: "db/csv_data/text_data.csv")
* File.Rails with join.root+"db/csv_data/text_data.csv"To combine.
path = File.join Rails.root, "db/csv_data/text_data.csv"
CSV.foreach(path, headers: true) do |row|
list << {
image: File.open("#{Rails.root}/#{row['image']}")
!!?? *I only put image in the list lol
}
end
puts "Start import process"
Text.create!(list)
puts "Import was successful"
end
Added below
lib/tasks/import_csv.rake
desc "Task to import TEXT data"
task text: :environment do
list = Import.csv_data(path: "db/csv_data/text_data.csv")
path = File.join Rails.root, "db/csv_data/text_data.csv"
CSV.foreach(path, headers: true) do |row|
list << {
image: File.open("#{Rails.root}/#{row['image']}"),
#Added below
genre: row["genre"],
title: row["title"],
content: row["content"]
}
end
puts "Start import process"
Text.create!(list)
puts "Import was successful"
end
Import again
rake import_csv:text
Start import process
Import was successful
You succeeded! Check if there is any content just in case.
console
pry(main)> Text.select("image")
.
.
.
.
#<Text:0x00007fb4e4c29f98 id: nil, image: "Slide 2.jpeg ">,
It's firmly in!
It's a rough content, but that's it.
It's nice to write it as an article because the information can be fixed.
https://remonote.jp/rails-carrierwave-seed
Recommended Posts