活动存储的 Blob 错误 Rails 5.2
Blob Error with Active Storage Rails 5.2
我刚刚升级了 5.1.4。应用程序升级到 5.2,我正在尝试将 Paperclip 换成 ActiveStorage。在尝试使用图像更新现有记录时,出现以下错误:
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
在我的模型中:
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
设置访问密钥
在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
寻找文件,但我在我的应用程序中看不到它?
我错过了什么?
已解决
虽然我已经 gem 'aws-sdk-s3', '~>1'
我没有 require: false
我遇到了完全相同的错误。对我来说,这是因为缺少 AWS S3 的凭据(credentials.yml.enc)
我有这个异常,但我的问题的原因是活动存储静默地无法从 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
它应该工作
我通过将 aws-sdk
从 2.2 更新为 3
来解决这个问题
我刚刚升级了 5.1.4。应用程序升级到 5.2,我正在尝试将 Paperclip 换成 ActiveStorage。在尝试使用图像更新现有记录时,出现以下错误:
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
在我的模型中:
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
在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
寻找文件,但我在我的应用程序中看不到它?
我错过了什么?
已解决
虽然我已经 gem 'aws-sdk-s3', '~>1'
我没有 require: false
我遇到了完全相同的错误。对我来说,这是因为缺少 AWS S3 的凭据(credentials.yml.enc)
我有这个异常,但我的问题的原因是活动存储静默地无法从 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
它应该工作
我通过将 aws-sdk
从 2.2 更新为 3