我已将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
运行到控制台中。
我收到以下错误:
导轨中止了!
不知道如何构建任务'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
阻止在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
阻止在'需要'
/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运行任务查看完整跟踪)
我不知道我做错了什么。
答案 0 :(得分:14)
你的问题在这里:
require "active_storage"
您需要引擎:
require "active_storage/engine"
答案 1 :(得分:0)
尝试在项目根目录中运行命令
bin/rails activestorage:install
或
rake active_storage:install
答案 2 :(得分:0)
当前的Rails官方文档讨论了新的应用程序。但
如果要升级5.2之前的应用程序,则必须添加或取消注释 application.rb
require "active_storage/engine"
对于新应用程序,它已经被添加。
答案 3 :(得分:0)
在配置中具有
require "active_storage/engine"
并运行此
bundle exec rails active_storage:install
这对我有用。我从Rails 5.1更新到6.0.2.1