LoginSignup
1
0

More than 1 year has passed since last update.

Carrierwaveで公開されているファイルURLからデータを保存する

Posted at

やりたいこと

既に公開されている画像のURLからCarrierwaveを介してファイルを保存する。
※ 試したのは画像だけですが他のファイルでもできそう

結論

# CarrierWave の Uploader
class AvatarUploader < CarrierWave::Uploader::Base
  storage :file
end

# ActiveRecord
class User < ApplicationRecord
  mount_uploader :avatar, AvatarUploader
end

User.create!(remote_avatar_url: "https://example.com/***.png")

remote_**_url に値を指定することによって解決しました。

よくよくみるとgithubのreadmeにもformの例として remote_**_url に値をセットする方法が紹介されてました。

ちゃんとおってないですが多分ここら辺のActiveRecordへのパッチでこのメソッドが早されているのかなという感じでした。

ダメなパターン

User.create!(avatar: URI.open("https://example.com/***.png"))

参考

1
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
1
0