简体   繁体   English

Active Storage Rails的Blob错误5.2

[英]Blob Error with Active Storage Rails 5.2

I've just upgraded a 5.1.4. 我刚刚升级了5.1.4。 app to 5.2 and am trying to swap out Paperclip for ActiveStorage. 应用程序到5.2,我正在尝试将Paperclip替换为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 无法自动加载常量ActiveStorage :: Blob :: Analyzable,预期/Users/Simon/.rvm/gems/ruby-2.4.0/gems/activestorage-5.2.0/app/models/active_storage/blob/analyzable.rb来定义它

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: 在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 我通过rails credentials:edit设置访问密钥rails credentials:edit

In development.rb: 在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. 我一直在阅读http://edgeapi.rubyonrails.org/classes/ActiveStorage/Blob/Analyzable.html,但这对我来说并没有多大意义。

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? 该错误让我在app/models/active_storage/blob/analyzable.rb查找该文件,但我在我的应用程序中看不到它?

What have I missed? 我错过了什么?

I've got exactly same error. 我有完全相同的错误。 For me it was because credentials for AWS S3 were missing (it was missing from credentials.yml.enc ) 对我而言,这是因为缺少AWS S3的凭据( 凭证中缺少凭证。

Resolved 解决

Whilst I already had gem 'aws-sdk-s3', '~>1' I didn't have require: false 虽然我已经有了gem 'aws-sdk-s3', '~>1'我没有require: false

I had this exception but the cause of my issues was that active storage was silently failing to load its configurations from storage.yml: 我有这个例外,但我的问题的原因是主动存储无法从storage.yml加载其配置:

Loading the file in a rails console shows the exception (whereas rails swallows the exception when loading active storage config): 在rails控制台中加载文件会显示异常(而rails在加载活动存储配置时会吞下异常):

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. 我的ERB插值评论无效。 Fixing this fixed my issue. 解决这个问题解决了我的问题。

Launch rails active_storage:install and rails db:migrate 启动rails active_storage:installrails db:migrate

Add this gem gem 'aws-sdk-s3', '~>1' and bundle 添加这个gem gem 'aws-sdk-s3', '~>1'bundle

Delete your credential.yml.enc and your master.key if exists 删除您的credential.yml.encmaster.key如果存在)

Launch EDITOR=vim rails credentials:edit and puts your credentials 启动EDITOR=vim rails credentials:edit并放置您的凭据

And normally rails s it should work 通常它应该工作的rails s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM