NameError Exception: uninitialized constant ActiveStorage::Attachment::ActiveStorage::Blob
Asked Answered
D

3

5

I am trying to use my Rails database and keep running into this error. The database was working previously with no active storage issues until 5 days ago when it stopped and I started seeing this error:

*** NameError Exception: uninitialized constant ActiveStorage::Attachment::ActiveStorage::Blob

I have a .jbuilder file that is where the error is happening,

json.array! @campaigns do |campaign|
    byebug
    json.extract! campaign, :id, :title, :description, :goal, :campaign_end, :user_id, :current_amount, :ends, :percent_complete, :amount_left_to_fund, :owner, :successful
    json.photoUrl url_for(campaign.photo)
end

Where it says url_for(campaign.photo) I get the error. However, just typing campaign.photo shows the ActiveStorage instance with the campaign it is attached to:

#<ActiveStorage::Attached::One:0x00007febc64f91b0 @name="photo", @record=#<Campaign id: 7, title: "Surgery", description: "Lorem ipsum dolor sit amet, consectetur adipiscing...", goal: 5000, user_id: 8, campaign_end: "2019-05-31 16:00:00", created_at: "2019-05-01 14:20:14", updated_at: "2019-05-01 14:20:14", successful: false>, @dependent=:purge_later>

I am just trying to get the url for this active storage photo. I tried googling this error but haven't seen anything with this error message. Any help would be much appreciate!

Donato answered 16/5, 2019 at 17:34 Comment(2)
Did you run rails active_storage:install ?Klan
Yes I did. Everything was working perfectly fine for about a week before I started seeing this error, rails active_storage:install was one of the first things I did. I have the active storage tables in my schema and the pictures are associated with the correct model in my back end.Donato
U
5

I just ran into this issue and solved with the simplest solution.

The storage.yml file had a block that ended up misaligned. Causing a yaml parse error and I imagine a whole list of load / initialize errors downstream.

You can verify your yaml with a linter or by simply running Rails.application.config_for(:storage) from rails console.

Unnecessary answered 27/7, 2019 at 6:57 Comment(1)
This was it, it was an error with the yaml file after all.Donato
G
1

My issue was also in storage.yml but it was because I had embedded Ruby in a section of code that had been commented, but the Ruby tag was not commented:

# digitalocean_spaces:
#   service: S3
#   access_key_id: <%= Rails.application.credentials.dig(:digitalocean_spaces, :access_key_id) %>
Grout answered 21/2, 2022 at 21:25 Comment(0)
A
0

In my case (Legacy APP using Rails 5.1.4 and ruby 2.4.2) what worked was altering the migration file created by the activestorage install from this:

ActiveStorage::CreateTables

To this:

ActiveStorageCreateTables

The file can be located in db/migrate/

Anthology answered 2/7, 2021 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.