LoginSignup
2
2

More than 1 year has passed since last update.

Can't resolve image into URL: to_model delegated to attachment, but attachment is nilの解決

Posted at

indexメソッドを使って一覧表示しようとしたときのエラーでした。

Image from Gyazo

文章を訳すと画像をURLに変換できなかった、モデルと紐づけられているが、nilだよって感じですかね。

要するにitem.imageがnilなんです。

1モデルの紐付けを見直す

app/models/item.rb
class Item < ApplicationRecord
  extend ActiveHash::Associations::ActiveRecordExtensions
  belongs_to :user
  has_one_attached :image

という感じで確かに紐付けしてる。

2画像がない投稿がある

今回はこっちでした。

Sequel Proで見てみると、itemの投稿は8個あったのに対して、画像は6個しかなかったんですね。

ということは2個は画像が紐づけられてないってことで、該当のレコードを削除して解決。

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