Rails apiアプリケーションを5.1から5.2に更新しました。Rails apiのみを使用しています。アクティブストレージを使用しようとしています。問題があると思います。 config.api_only = true
の行config/application.rb
が原因です。
私はたくさんのグーグルをしましたが、Rails apiでアクティブストレージを使用する方法は何も見つかりませんでした。
これが私のGemfile
です:
source 'https://rubygems.org'
Ruby '2.5.1'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
gem 'Rails', '~> 5.2.0'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '~> 3.7'
gem 'rack-cors'
# Auth
gem 'bcrypt'
gem 'jwt'
#Media file upload
gem 'activestorage'
# gem 'devise', '4.4.3'
# gem 'devise-jwt', '~> 0.5.6'
gem 'active_model_serializers', '~> 0.10.0', require: true
# backgroud jobs
gem 'sidekiq'
# fast boot
gem 'bootsnap', require: false
group :development, :test do
gem 'rspec-Rails', '~> 3.7'
gem 'brakeman'
gem 'rubocop'
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'foreman'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'pry'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'annotate'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
これが私のconfig/application.rb
です
require_relative 'boot'
require "Rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
require "active_storage"
# require "sprockets/railtie"
require "Rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CelebrationBackend
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Only loads a smaller set of middleware suitable for API only apps.
# Middleware like session, flash, cookies can be added back manually.
# Skip views, helpers and assets when generating a new resource.
config.api_only = true
config.active_job.queue_adapter = :sidekiq
config.autoload_paths << Rails.root.join('lib')
end
end
コンソールにRails activestorage:install
を実行している間。次のエラーが発生します:
Railsが中止されました!タスク「activestorage:install」をビルドする方法がわからない(--tasksを参照)/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/railties-5.2.0/lib/Rails/commands /rake/rake_command.rb:23:in block in perform' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/railties-5.2.0/lib/Rails/commands/rake/rake_command.rb:20:in
perform'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/railties-5.2.0/lib/Rails/command.rb:48:in invoke' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/railties-5.2.0/lib/Rails/commands.rb:18:in
'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in require' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in
block in require_with_bootsnap_lfi'/home/sourabh /.rvm/gems/Ruby-2.5.1@celeb/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in register' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in
require_with_bootsnap_lfi '/ home/sourabh/.rvm/gems/Ruby-2.5 .1 @ celeb/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in require' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in
block in require'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/ gems/activesupport-5.2.0/lib/active_support /dependencies.rb:249:in load_dependency' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in
require '/ home/sourabh/dev/celebration/bin/Rails:9:in <top (required)>' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/lib/spring/client/Rails.rb:28:in
load'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/lib/spring/client/Rails.rb:28:in call' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/lib/spring/client/command.rb:7:in
call'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb /gems/spring-2.0.2/lib/spring/client.rb:30:in run' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/bin/spring:49:in
'/home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/lib/ spring/binstub.rb:31:in load' /home/sourabh/.rvm/gems/Ruby-2.5.1@celeb/gems/spring-2.0.2/lib/spring/binstub.rb:31:in
'/ home/sourabh/dev/celebration/bin/spring:15:in require' /home/sourabh/dev/celebration/bin/spring:15:in
' bin/Rails:3:in load' bin/Rails:3:in
'(-traceを指定してタスクを実行することで完全なトレースを参照)
私は自分が間違っていることを正確に理解していません。
あなたの問題はここにあります:
require "active_storage"
エンジンが必要です:
require "active_storage/engine"
現在のRails公式ドキュメントでは新しいアプリケーションについて説明しています。しかし5.2より前のアプリをアップグレードする場合、追加またはコメント解除application.rb
のフィールド
require "active_storage/engine"
新しいアプリケーションの場合、これはすでに追加されています。
プロジェクトルートでコマンドを実行してみてください
bin/Rails activestorage:install
または
rake active_storage:install
設定にこれがある
require "active_storage/engine"
そしてこれを実行します
bundle exec Rails active_storage:install
これは私のために働いた。 Rails 5.1から6.0.2.1に更新しました