Can't resolve image into URL: to_model delegated to attachment, but attachment is nil Rails 5.2

落爺英雄遲暮 提交于 2019-12-05 11:58:23

You can get the error message "Can't resolve image into URL: to_model delegated to attachment, but attachment is nil" if you are trying to show in your view an attachment that does not exist:

<%= image_tag(@user.avatar) %>

to avoid error you should do this:

<%= image_tag(@user.avatar) if @user.avatar.attached? %>

It seems you are missing configuration (because you don't mention it):

You must declare Active Storage services in config/storage.yml

Example from docs:

local:
  service: Disk
  root: <%= Rails.root.join("storage") %>

test:
  service: Disk
  root: <%= Rails.root.join("tmp/storage") %>

amazon:
  service: S3
  access_key_id: ""
  secret_access_key: ""

and you must tell Active Storage which service to use by setting Rails.application.config.active_storage.service

Because each environment will likely use a different service, it is recommended to do this on a per-environment basis. To use the disk service from the previous example in the development environment, you would add the following to config/environments/development.rb:

# Store files locally.
config.active_storage.service = :local
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!