0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【CarrierWave】Nil location provided. Can't build URIというエラーの対処

Last updated at Posted at 2022-05-03

エラーの内容

AcitiveAdminで作成した管理画面の中の画像なしの投稿の詳細画面を見にいくと、以下のエラー達が出た。

ArgumentError in Admin::News#show

Nil location provided. Can't build URI.

解決策(公式ドキュメントに従ったもの)

こちらのエラーも調べると、表示するデータがないのでエラーが出ているとのことだった。
画像の設定がない場合の表示を指定する解決策が提示されている記事があるかと思います。
エラー原因と解決策は他の記事に譲るとして、CarrierWaveの公式ドキュメントに解決策の部分
について言及されていた部分を載せておきたいと思います。

Providing a default URL

In many cases, especially when working with images, it might be a good idea to provide a default url, a fallback in case no file has been uploaded. You can do this easily by overriding the default_url method in your uploader:(DeepL和訳:デフォルトURLの提供。多くの場合、特に画像を扱う場合、ファイルがアップロードされなかった場合の予備として、デフォルトのurlを提供するのは良い考えかもしれません。アップローダーのdefault_urlメソッドをオーバーライドすることで、簡単にこれを行うことができます。)

class MyUploader < CarrierWave::Uploader::Base
  def default_url(*args)
    "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end
end

公式ドキュメント該当部分のURL:
https://github.com/carrierwaveuploader/carrierwave#providing-a-default-url

image_uploader.rbをみにいくと、上記のドキュメントのようにメソッド(default_url)とその中身が記述がされており、それらはコメントアウトされていたので解除しました。
そうすると、当初のエラーが解消されました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?