LoginSignup
1
0

More than 1 year has passed since last update.

[Rails] ArgumentError Nil location provided. Can't build URI. が出た時の対処法 

Last updated at Posted at 2021-09-03

ArgumentError

スクリーンショット 2021-09-03 22.04.57.png

Nil location provided. Can't build URI.
『提供された場所はありません。 URIを構築できません。』と怒られました:japanese_ogre:

SQLで確認

スクリーンショット 2021-09-03 22.37.32.png

今回、怒られたproduct.image_idを確認したらnilであった

解決方法

当該のviewページを確認したら、nilの場合の条件をつけていませんでした....

      <div class="mt-3" >
      <%= image_tag attachment_url(@product, :image, :fill, 250, 200 ) %>
      </div>

      <% if @product.image_id.blank? %>
        <%= attachment_image_tag @product, :image, fallback:"no-image.png", size: '220x185' %>
      <% else%>
        <%= image_tag attachment_url(@product, :image, :fill, 250, 200 ) %>
      <% end %>

blank?メソッドは真偽値を返すので、if文を使ってオブジェクトが空白の場合と空白ではない場合で処理を分ける際に便利です。

if オブジェクト.blank?
  # オブジェクトが空白の場合の処理
else
  # オブジェクトが空白ではない場合の処理
end

この要領で画像:有、無しの処理を分岐しました!

エラーを切り分けていくことで無事解決できました!:heart_eyes:

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