[英]Rails 5.2 Active storage Unable to autoload constant ActiveStorage::Blob::Analyzable
[英]Blob Error with Active Storage Rails 5.2
我刚刚升级了5.1.4。 应用程序到5.2,我正在尝试将Paperclip替换为ActiveStorage。 在尝试使用图像更新现有记录时,我收到以下错误:
无法自动加载常量ActiveStorage :: Blob :: Analyzable,预期/Users/Simon/.rvm/gems/ruby-2.4.0/gems/activestorage-5.2.0/app/models/active_storage/blob/analyzable.rb来定义它
在我的模型中:
has_one_attached :pic
在我的控制器中:
...
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
在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
我通过rails credentials:edit
设置访问密钥rails credentials:edit
在development.rb中:
config.active_storage.service = :amazon
在我看来:
<%= image_tag @gin.pic, class: "border shadow-lg" %>
我一直在阅读http://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob/Analyzable.html,但这对我来说并没有多大意义。
该错误让我在app/models/active_storage/blob/analyzable.rb
查找该文件,但我在我的应用程序中看不到它?
我错过了什么?
我有完全相同的错误。 对我而言,这是因为缺少AWS S3的凭据( 凭证中缺少凭证。 )
解决
虽然我已经有了gem 'aws-sdk-s3', '~>1'
我没有require: false
我有这个例外,但我的问题的原因是主动存储无法从storage.yml加载其配置:
在rails控制台中加载文件会显示异常(而rails在加载活动存储配置时会吞下异常):
YAML.load(ERB.new(File.read("#{Rails.root}/config/storage.yml")).result)
NoMethodError: undefined method `[]' for nil:NilClass
我的ERB插值评论无效。 解决这个问题解决了我的问题。
启动rails active_storage:install
和rails db:migrate
添加这个gem gem 'aws-sdk-s3', '~>1'
并bundle
删除您的credential.yml.enc
和master.key
如果存在)
启动EDITOR=vim rails credentials:edit
并放置您的凭据
通常它应该工作的rails s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.