ruby-on-rails 无法自动加载常量ActiveStorage::Blob::Analyzable Rails 5.2、AWS S3和ActiveStorage错误

7jmck4yq  于 2023-05-23  发布在  Ruby
关注(0)|答案(7)|浏览(141)

我一直在与这个家伙斗争了一段时间,并做了所有的谷歌在它(herehere,和许多同样无用的其他人),但无济于事。
官方的错误是这样的,在我的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,都是通过ActiveStorage创建的。这些关系都在blog模型中:

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

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

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

以这个作为我的参数:

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

我按照this教程使用S3设置ActiveStorage,并按照this one设置ActiveStorage。
我的storage.yml看起来像这样:

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"] %>

我的新博客看起来像这样:

<%= 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 %>

我确实输入了配置键到Heroku,但由于这个错误是在本地主机上,我看不出有什么不同。我还确保安装了minimagick。
有人能看出哪里出问题了吗?我真的越来越讨厌ActiveStorage,因为它给了我这么多问题……

其他信息

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
qv7cva1a

qv7cva1a1#

修复此问题的方法是按顺序运行以下内容。

rails active_storage:install
rake db:migrate

如果你得到错误,* 无法保存新关联的image_attachment*,这主要是因为has_one关联。要修复它,您应该执行以下操作

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

cu6pst1q2#

在我的例子中,问题出在自动加载和重新加载常量(Zeitwerk模式)(Rails6.1)
我解决了更改文件:/config/environments/development.rb
config.autoloader = :classic
(来源:https://github.com/rails/rails/issues/38681

yzxexxkh

yzxexxkh3#

我知道这个问题已经有了答案,但是我偶然发现了同样的情况,重新安装active_storage和迁移数据库都不起作用。
我偶然发现了这个论坛:https://groups.google.com/forum/#!topic/rubyonrails-talk/RaFBG 6 wi 2K 0,找到了一个把我推向正确方向的答案。
我的问题是我的storage.yml文件不正确,甚至不是有效的yml。尝试通过在线yml验证器运行storage.yml文件:http://www.yamllint.com/
它未能验证,我能够从那里找出我的问题。

xqkwcwgp

xqkwcwgp4#

我在做了这件事之后:

rails active_storage:install
rake db:migrate

问题是我没有正确的“config/master.key”文件用于当前的“config/credentials.yml.enc”文件。

qyuhtwio

qyuhtwio5#

由于某些原因,config.autoloader = :classic选项中断了我的应用程序中其他一些类的加载;然而,添加一个初始化器,下面的工作(Rails 6.0.4.7):

Rails.application.reloader.to_prepare do
  ActiveStorage::Blob
end
ojsjcaue

ojsjcaue6#

感谢托马斯和user6374022的提醒,我发现我的YAML在开发中没有正确解析。
storage.yml:

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

google:
  service: GCS
  project: app
  bucket: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :bucket) %>
  credentials:
    type: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :type) %>
    project_id: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :project_id) %>
    private_key_id: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :private_key_id) %>
    private_key: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :private_key).dump %>
    client_email: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :client_email) %>
    client_id: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :client_id) %>
    auth_uri: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :auth_uri) %>
    token_uri: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :token_uri) %>
    auth_provider_x509_cert_url: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :auth_provider_x509_cert_url) %>
    client_x509_cert_url: <%= Rails.application.credentials.dig(Rails.env.to_sym, :google, :client_x509_cert_url) %>

在我的例子中,private_key行在开发中没有正确解析,因为dig函数返回null,而dump函数不存在nil。
我用下面的话纠正了它:

private_key: <%= (Rails.application.credentials.dig(Rails.env.to_sym, :google, :private_key) || '').dump %>
vdzxcuhz

vdzxcuhz7#

别忘了你也可以用Rails.logger调试storage.yml。更改后始终重新启动服务器。

# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
<% Rails.logger.info "STORAGE.YML" %>
<% Rails.logger.info Rails.application.credentials.dig(:s3, :key) %>
<% Rails.logger.info Rails.application.credentials.dig(:s3, :secret) %>
s3:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:s3, :key) %>
  secret_access_key: <%= Rails.application.credentials.dig(:s3, :secret) %>

相关问题