本文介绍了无法使用 Rails 5.2、AWS S3 和 ActiveStorage 自动加载常量 ActiveStorage::Blob::Analyzable 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经和这个家伙战斗了一段时间,并且已经完成了所有的谷歌搜索(此处此处,以及许多同样无益的其他人)但无济于事.

官方的错误是这样的,在我的create方法的第一行调用:

无法自动加载常量 ActiveStorage::Blob::Analyzable,预期/Users/lizbayardelle/.rvm/gems/ruby-2.5.0/gems/activestorage-5.2.1/app/models/active_storage/blob/analyzable.rb定义它

我正在创建一个 blog 模型,其中 has_one_attached :pdf 和一个 :image,都通过活动存储.这些关系都在blog模型中:

class 博客 <申请记录归属地:用户has_one_attached :图像has_one_attached :pdf结尾

我的控制器 blogs#create 模型在这里:

 def 创建@blog = Blog.new(blog_params)@blog.user_id = current_user.id如果@[email protected]_on = DateTime.current结尾response_to do |格式|如果@[email protected](params[:image])@blog.pdf.attach(params[:pdf])format.html { redirect_to @blog,注意:'博客已成功创建.'}format.json { 渲染:显示,状态::已创建,位置:@blog }别的format.html { 渲染:新}format.json { 渲染 json: @blog.errors, 状态: :unprocessable_entity }结尾结尾结尾

以此作为我的参数:

def blog_paramsparams.require(:blog).permit(:title, :teaser, :body, :cta, :category, :linked_module, :published, :published_on, :user_id, :image, :pdf)结尾

我按照教程来设置 ActiveStorageS3 和一般设置 ActiveStorage.

我的 storage.yml 看起来像这样:

 test: service: Disk root: 本地:服务:磁盘根目录:<%= Rails.root.join("storage") %># 使用 rails credentials:edit 设置 AWS 机密(如 aws:access_key_id|secret_access_key) amazon: service: S3 access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>区域:us-west-1 存储桶:*存储桶名称*在我的 `secrets.yml` 中使用这个(如果它有所不同,它是 GitIgnored):发展:secret_key_base:这里的实际密钥AWS_ACCESS_KEY_ID:这里的实际密钥AWS_SECRET_ACCESS_KEY:这里的实际密钥测试:secret_key_base:这里的实际密钥# 不要在存储库中保留生产机密,# 而是从环境中读取值.生产:AWS_ACCESS_KEY_ID: <%= ENV["AWS_ACCESS_KEY_ID"] %>AWS_SECRET_ACCESS_KEY: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>recaptcha_site_key: <%= ENV["RECAPTCHA_SITE_KEY"] %>recaptcha_secret_key: <%= ENV["RECAPTCHA_SECRET_KEY"] %>

我的新博客表单如下所示:

<%= simple_form_for(@blog) do |f|%><%= f.error_notification %><%= f.error_notification 消息:f.object.errors[:base].to_sentence if f.object.errors[:base].present?%><div class="form-inputs"><div class="form-group"><%= f.label :category %><%= f.select :category, options_for_select(['General', 'House', 'Spouse', 'Kids', 'Other'], { class: "form-control" }) %>

<div class="form-group"><%= f.label :title %><%= f.text_field :title, class: "form-control" %>

<div class="form-group"><%= f.label :teaser %><%= f.text_area :teaser, class: "form-control" %>

<div class="form-group"><%= f.label :body %><%= f.trix_editor :body %>

<div class="form-group"><%= f.label :cta%><%= f.text_field :cta, class: "form-control" %>

<div class="form-group"><%= f.label :linked_module %><%= f.text_field :linked_module, class: "form-control" %>

<div class="form-group"><%= f.label博客图片"%><br/><%= f.file_field :image %>

<div class="form-group"><%= f.label "Linked PDF" %><br/><%= f.file_field :pdf %>

<div class="form-group text-center"><%= f.input :published %>

<div class="form-actions text-center"><%= f.button :submit %>

<%结束%>

我确实向 Heroku 输入了配置键,但由于此错误发生在 localhost 上,因此我看不出这有什么不同.我还确保安装了 minimagick.

谁能看到这里出了什么问题?在 ActiveStorage 给我带来的所有问题之后,我真的越来越讨厌它了...

附加信息

这是我的 Gemfile:来源https://rubygems.org"git_source(:github) { |回购|"https://github.com/#{repo}.git" }红宝石'2.5.0'gem 'rails', '~>5.2.0'宝石'美洲狮','~>3.11'gem 'sass-rails', '~>5.0'gem 'uglifier', '>= 1.3.0'gem '咖啡轨','~>4.2'gem 'jbuilder', '~>2.5'宝石'mini_magick'宝石'jquery-rails'宝石设计"gem 'bootsnap'gem 'bootstrap', '~>4.1.3'宝石链轮导轨"gem 'bootstrap-sass'gem 'bcrypt', '~>3.1.7'gem 'friendly_id', '~>5.1.0'宝石条纹"宝石费加罗"gem 'magnific-popup-rails', '~>1.1.0'宝石'simple_form'gem 'acts-as-taggable-on'gem 'aws-sdk' , '~>3'gem 'aws-sdk-s3',需要:false宝石'simple_form_extension'gem 'recaptcha',需要:recaptcha/rails"gem 'font-awesome-rails'宝石'trix',git:'https://github.com/bcoia/trix.git'组:生产做宝石 'pg', '~>0.20.0'宝石'rails_12factor'结尾组:开发,:测试做gem 'byebug', 平台: [:mri, :mingw, :x64_mingw]宝石'sqlite3'结尾组:开发做宝石'sqlite3'宝石网络控制台",> = 3.3.0"宝石'听','> = 3.0.5','2.0.0'结尾组:测试做宝石'sqlite3'宝石'水豚','> = 2.15','
解决方案

解决这个问题是按顺序运行以下.

rails active_storage:install耙数据库:迁移

如果您收到错误消息,无法保存新的关联图像附件,主要是因为 has_one 关联.要修复它,您应该执行以下操作

@[email protected](params[:image])

I've been battling this guy for a while and have done all the Googlies on it (here, here, and many equally-unhelpful others) but to no avail.

The official error is this, called on the first line of my create method:

I'm creating a blog model, which has_one_attached :pdf and one :image, both through ActiveStorage. These relationships are all in the blog model:

class Blog < ApplicationRecord
  belongs_to :user
  has_one_attached :image
  has_one_attached :pdf
end

My controller blogs#create model is here:

  def create
    @blog = Blog.new(blog_params)
    @blog.user_id = current_user.id
    if @blog.published
      @blog.published_on = DateTime.current
    end

    respond_to do |format|
      if @blog.save
        @blog.image.attach(params[:image])
        @blog.pdf.attach(params[:pdf])
        format.html { redirect_to @blog, notice: 'Blog was successfully created.' }
        format.json { render :show, status: :created, location: @blog }
      else
        format.html { render :new }
        format.json { render json: @blog.errors, status: :unprocessable_entity }
      end
    end
  end

With this as my params:

def blog_params
  params.require(:blog).permit(:title, :teaser, :body, :cta, :category, :linked_module, :published, :published_on, :user_id, :image, :pdf)
end

I followed this tutorial to set up ActiveStorage with S3 and this one to set up ActiveStorage in general.

My storage.yml looks like this:

    test:   service: Disk   root: <%= Rails.root.join("tmp/storage") %>

    local:   service: Disk   root: <%= Rails.root.join("storage") %>

    # Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) amazon:   service: S3   access_key_id: <%= ENV["AWS_ACCESS_KEY_ID"] %>   secret_access_key: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>   region: us-west-1   bucket: *bucket name*

With this in my `secrets.yml` (which is GitIgnored, if it makes a difference):
development:
  secret_key_base: actual key here
  AWS_ACCESS_KEY_ID: actual key here
  AWS_SECRET_ACCESS_KEY: actual key here
test:
  secret_key_base: actual key here

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  AWS_ACCESS_KEY_ID: <%= ENV["AWS_ACCESS_KEY_ID"] %>
  AWS_SECRET_ACCESS_KEY: <%= ENV["AWS_SECRET_ACCESS_KEY"] %>
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
  recaptcha_site_key: <%= ENV["RECAPTCHA_SITE_KEY"] %>
  recaptcha_secret_key: <%= ENV["RECAPTCHA_SECRET_KEY"] %>

My new blog form looks like this:

<%= simple_form_for(@blog) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <div class="form-inputs">
    <div class="form-group">
      <%= f.label :category %>
      <%= f.select :category, options_for_select(['General', 'House', 'Spouse', 'Kids', 'Other'], { class: "form-control" }) %>
    </div>
    <div class="form-group">
      <%= f.label :title %>
      <%= f.text_field :title, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= f.label :teaser %>
      <%= f.text_area :teaser, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= f.label :body %>
      <%= f.trix_editor :body %>
    </div>
      <div class="form-group">
      <%= f.label :cta %>
      <%= f.text_field :cta, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= f.label :linked_module %>
      <%= f.text_field :linked_module, class: "form-control" %>
    </div>
    <div class="form-group">
      <%= f.label "Blog Image" %><br />
      <%= f.file_field :image %>
    </div>
    <div class="form-group">
      <%= f.label "Linked PDF" %><br />
      <%= f.file_field :pdf %>
    </div>
    <div class="form-group text-center">
      <%= f.input :published %>
    </div>
  </div>

  <div class="form-actions text-center">
    <%= f.button :submit %>
  </div>
<% end %>

I did input the config keys to Heroku, but since this error is on localhost I can't see that making a difference. I also made sure minimagick is installed.

Can anyone see what's going wrong here? I'm really growing to hate ActiveStorage after all the issues it's giving me...

ADDITIONAL INFORMATION

Here's my Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.5.0'

gem 'rails', '~> 5.2.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'jbuilder', '~> 2.5'
gem 'mini_magick'
gem 'jquery-rails'
gem 'devise'
gem 'bootsnap'
gem 'bootstrap', '~> 4.1.3'
gem 'sprockets-rails'
gem 'bootstrap-sass'
gem 'bcrypt', '~> 3.1.7'
gem 'friendly_id', '~> 5.1.0'
gem 'stripe'
gem 'figaro'
gem 'magnific-popup-rails', '~> 1.1.0'
gem 'simple_form'
gem 'acts-as-taggable-on'
gem 'aws-sdk' , '~> 3'
gem 'aws-sdk-s3', require: false
gem 'simple_form_extension'
gem 'recaptcha', require: "recaptcha/rails"
gem 'font-awesome-rails'
gem 'trix', git: 'https://github.com/bcoia/trix.git'

group :production do
  gem 'pg', '~> 0.20.0'
  gem 'rails_12factor'
end

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3'
end

group :development do
  gem 'sqlite3'
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  gem 'sqlite3'
  gem 'capybara', '>= 2.15', '< 4.0'
  gem 'selenium-webdriver'
  gem 'chromedriver-helper'
end

gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]


  [1]: https://medium.com/alturasoluciones/setting-up-rails-5-active-storage-with-amazon-s3-3d158cf021ff
  [2]: https://edgeguides.rubyonrails.org/active_storage_overview.html
解决方案

Fix for this is to run the following in the order.

rails active_storage:install
rake db:migrate

And if you get the error, Failed to save the new associated image_attachment, it's mostly because of has_one association. To fix it, you should do the following

@blog.image.purge
@blog.image.attach(params[:image])

这篇关于无法使用 Rails 5.2、AWS S3 和 ActiveStorage 自动加载常量 ActiveStorage::Blob::Analyzable 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 11:41