I have a project that is on rails 5.1.2
and I am trying to implement Active Storage on it, following couple of tutorials online I am able to setup the Active Storage and I can see the data being saved inside the active_storage_blobs
and active_storage_attachments
table.
I also confirmed by running user.avatar.attached?
and in response I got true so things are working.
What I am having problem with is displaying an image in view, I have tried
<%= image_tag(url_for(user.avatar)) %>
NoMethodError: undefined method `active_storage_attachment_path'
What I have done to setup Active Storage
Ran the install command
rails active_storage:install
This gave error Don't know how to build task 'active_storage:install'
but the following command worked
rails activestorage:install
I have added gem 'activestorage'
inside gemfile
This is what I have inside my storage_services.yml
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
Inside my development.rb
I have added
config.active_storage.service = :local
Inside my User
model I have added has_one_attached :avatar
Inside the controller on update I added the attachment code
user.avatar.attach(params[:user][:avatar])
Yet I am not able to display it,
What am I missing here? What do I need to do to get the image to display in the view? Why am i getting this error
NoMethodError: undefined method `active_storage_attachment_path'
Step 1 Import the Image File The goat image that will be added to the about page can be downloaded from this URL. To add the goat image to project, we save the image file in the project directory app/assets/images/ . Putting the image in this directory will make the image available for the app's webpages to display.
By default in the development environment, Active Storage stores all uploaded images on your local disk in the storage subdirectory of the Rails application directory. That's the file you uploaded!
To do this, go to app/assets/images and add all your file to the images folder. Though this is fine for small projects, you may want to better organize your image files if you're going to build a large project that can have hundreds of images.
You are doing wrong, you need to replace following code
<%= image_tag(url_for(user.avatar)) %>
with
<%= image_tag(user.avatar) %>
url_for is used to generate exact url not image tag.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With