问题
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?
回答1:
I've got exactly same error. For me it was because credentials for AWS S3 were missing (it was missing from credentials.yml.enc)
回答2:
Resolved
Whilst I already had gem 'aws-sdk-s3', '~>1'
I didn't have require: false
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/50307502/blob-error-with-active-storage-rails-5-2