RailsでseedデータにCarrierWaveを使って複数の画像を投入できない
Q&A
Closed
解決したいこと
RailsでseedデータにCarrierWaveを使って複数の画像を投入できるようにしたい。
発生している問題・エラー
development.rb
5.times do |n|
Product.create!(
name: "test #{n} ",
price: 300,
images: [./public/uploads/product/images/3/IMG_1618.jpeg],
introduction: "あ" * 100
)
end
$ rails db:migrate:reset
$ rails db:migrate
$ rails db:seed
Running via Spring preloader in process 3977
rake aborted!
SyntaxError: /Users/username/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:29: syntax error, unexpected '.', expecting ']'
images: [./public/uploads/product/image...
^
/Users/username/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:29: syntax error, unexpected ']', expecting `end'
...product/images/3/IMG_1618.jpeg],
... ^
/Users/username/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:31: syntax error, unexpected ')', expecting end-of-input
/Users/username/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds.rb:1:in `<main>'
<internal:/Users/username/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/Users/username/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
-e:1:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
該当するソースコード
product.rb
class Product < ApplicationRecord
mount_uploaders :images, ProductUploader
serialize :images, JSON
validates :images, presence: true
validates :name, presence: true, uniqueness: true, length: { maximum: 100 }
validates :price, presence: true, numericality: { greater_than_or_equal_to: 1, less_than_or_equal_to: 10000 }
default_scope -> { order(created_at: :desc) }
end
schema.rb
create_table "products", force: :cascade do |t|
t.string "name"
t.string "price"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.text "introduction"
t.string "images"
end
自分で試したこと
seedファイルの書き方を試しました。
1
development.rb
5.times do |n|
Product.create!(
name: "test #{n} ",
price: 300,
images: [IMG_1618.jpeg],
introduction: "あ" * 100
)
end
エラー
rake aborted!
SyntaxError: /Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:29: syntax error, unexpected '.', expecting ']'
images: [./public/uploads/product/image...
^
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:29: syntax error, unexpected ']', expecting `end'
...product/images/3/IMG_1618.jpeg],
... ^
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:31: syntax error, unexpected ')', expecting end-of-input
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds.rb:1:in `<main>'
<internal:/Users/mochizukikouki/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/Users/mochizukikouki/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
-e:1:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
2
development.rb
5.times do |n|
Product.create!(
name: "test #{n} ",
price: 300,
images: File.open("./app/assets/images/sioyaki.jpg"),
introduction: "あ" * 100
)
end
エラー
$ rails db:seed
Running via Spring preloader in process 4421
rake aborted!
ArgumentError: invalid byte sequence in UTF-8
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:26:in `block in <main>'
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:25:in `times'
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds/development.rb:25:in `<main>'
/Users/mochizukikouki/Documents/homepage/homepage_on_ruby_on_rails/homepage/db/seeds.rb:1:in `<main>'
<internal:/Users/mochizukikouki/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
<internal:/Users/mochizukikouki/.rbenv/versions/3.1.2/lib/ruby/3.1.0/rubygems/core_ext/kernel_require.rb>:85:in `require'
-e:1:in `<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
お力をお貸しください
よろしくお願いします。
0