Rails: Can't resolve image into URL: to_model delegated to attachment, but attachment is nil Rails 5.2
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