Blob Error with Active Storage Rails 5.2
Asked Answered
S

5

5

I've just upgraded a 5.1.4. app to 5.2 and am trying to swap out Paperclip for ActiveStorage. At the moment when trying to update an existing record with an image, I get the following error:

Unable to autoload constant ActiveStorage::Blob::Analyzable, expected /Users/Simon/.rvm/gems/ruby-2.4.0/gems/activestorage-5.2.0/app/models/active_storage/blob/analyzable.rb to define it

In my model:

has_one_attached :pic

In my controller:

...

  def update
    respond_to do |format|
      if @gin.update(gin_params)
        format.html { redirect_to @gin, notice: 'Gin was successfully updated.' }
        format.json { render :show, status: :ok, location: @gin }
      else
        format.html { render :edit }
        format.json { render json: @gin.errors, status: :unprocessable_entity }
      end
    end
  end

...

 def gin_params   params.require(:gin).permit(:name, :text, :snippet,
 :pic, :slug, :abv, distillery_attributes: [:id, :name], botanical_ids:
 []) end

In storage.yml:

   amazon:
    service: S3
    access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
    secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
    region: xx-xxxx-x
    bucket: xxxxxxx

I set the access keys via rails credentials:edit

In development.rb:

 config.active_storage.service = :amazon

In my views:

<%= image_tag @gin.pic, class: "border shadow-lg" %>

I've been reading though http://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob/Analyzable.html but it doesn't make too much sense to me.

The error has made me look for the file at app/models/active_storage/blob/analyzable.rb but I can't see it in my app?

What have I missed?

Socialization answered 12/5, 2018 at 14:50 Comment(1)
Do you have gem "aws-sdk-s3", require: false in your Gemfile? And did you do rails active_storage:install, rake db:migrate?Garrow
S
1

Resolved

Whilst I already had gem 'aws-sdk-s3', '~>1' I didn't have require: false

Socialization answered 13/5, 2018 at 8:35 Comment(0)
D
2

I've got exactly same error. For me it was because credentials for AWS S3 were missing (it was missing from credentials.yml.enc)

Dextro answered 4/7, 2018 at 21:16 Comment(0)
S
1

Resolved

Whilst I already had gem 'aws-sdk-s3', '~>1' I didn't have require: false

Socialization answered 13/5, 2018 at 8:35 Comment(0)
G
1

I had this exception but the cause of my issues was that active storage was silently failing to load its configurations from storage.yml:

Loading the file in a rails console shows the exception (whereas rails swallows the exception when loading active storage config):

YAML.load(ERB.new(File.read("#{Rails.root}/config/storage.yml")).result)
NoMethodError: undefined method `[]' for nil:NilClass

I had a comment with an ERB interpolation that was invalid. Fixing this fixed my issue.

Gonroff answered 19/12, 2018 at 21:28 Comment(0)
P
0

Launch rails active_storage:install and rails db:migrate

Add this gem gem 'aws-sdk-s3', '~>1' and bundle

Delete your credential.yml.enc and your master.key if exists

Launch EDITOR=vim rails credentials:edit and puts your credentials

And normally rails s it should work

Pigmentation answered 17/1, 2019 at 10:33 Comment(0)
V
0

I fixed this by updating aws-sdk from 2.2 to 3

Volva answered 10/9, 2021 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.