LoginSignup
2
0

More than 5 years have passed since last update.

Refileでフォームを使わずにファイルを保存する

Last updated at Posted at 2018-05-31

Seedデータなど入れる際にpublicフォルダなどにサンプル画像を用意して、そこからからデータを引っ張ってきたい場合はよくあると思います。

こんな感じでシンプルに書くことができました。

post = Post.new
image = "ファイルの絶対パス"
post.image = File.open(image)
post.save

もしファイルが別のモデルに保存されてる場合は

original_image = Image.find(1)
image = original_image.image.current_path

こんな感じ

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0