启动服务器时,Rails + ActiveStorage问题=未定义的方法“ has_one_attached”

时间:2018-11-20 22:39:29

标签: ruby-on-rails server

我对特定型号有疑问。当我启动Ruby on Rails服务器rails s时,出现错误消息undefined method has_one_attached'`。这是一件奇怪的事情:

如果我注释掉has_one_attached :avatar并启动服务器,则启动正常。然后,我可以取消注释has_one_attached :avatar,保存文件并加载应用程序ActiveStorage功能。

但是我不想每次启动服务器时都注释掉所有活动的存储功能。

Rails版本= 5.2.1

Ruby版本= 2.4.4

这是模型:

class User < ApplicationRecord
  has_person_name

  rolify
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  # For reasons I can't understand. This doesn't work here. I'm developing it in organizations for the time being.
  has_one_attached :avatar
  has_and_belongs_to_many :organizations
end

这是application.rb文件:

require_relative 'boot'
require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module *ProjectName*
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2

    config.generators do |g|
      g.orm                 :active_record
      g.template_engine     :erb
      g.test_framework      false
      g.stylesheets         false
      g.javascripts         false
      g.helper              false
      g.scaffold_stylesheet false
    end
  end
end

这是错误消息:

$ rails s
/Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/pry-rails-0.3.6/lib/pry-rails/prompt.rb:36: warning: constant Pry::Prompt::MAP is deprecated
=> Booting Puma
=> Rails 5.2.1 application starting in development 
=> Run `rails server -h` for more startup options
Exiting
/Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activerecord-5.2.1/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_one_attached' for User (call 'User.connection' to establish a connection):Class (NoMethodError)
Did you mean?  has_person_name
    from /Users/silverSheep/Desktop/code/withBetter/projectName/app/models/user.rb:3:in `<class:User>'
    from /Users/silverSheep/Desktop/code/withBetter/projectName/app/models/user.rb:1:in `<main>'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in `load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:472:in `block in load_file'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:657:in `new_constants_in'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:471:in `load_file'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:369:in `block in require_or_load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `block in load_interlock'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:37:in `load_interlock'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:352:in `require_or_load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:46:in `block in require_or_load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:45:in `require_or_load'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:506:in `load_missing_constant'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:58:in `block in load_missing_constant'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:16:in `allow_bootsnap_retry'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/bootsnap-1.3.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:57:in `load_missing_constant'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:193:in `const_missing'
    from /Users/silverSheep/.rvm/gems/ruby-2.4.4/gems/activesupport-

...(shortened for brevity)....

    from bin/rails:3:in `load'
    from bin/rails:3:in `<main>'

1 个答案:

答案 0 :(得分:0)

运行rails active_storage:install并将以下内容添加到 config / environments / development.rb 文件中,以将文件存储在本地: config.active_storage.service = :local

还要确保将新参数添加到 application_controller.rb 的允许参数中:

before_action :configure_permitted_parameters, if: :devise_controller?

protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:avatar])
    devise_parameter_sanitizer.permit(:account_update, keys: [:avatar])
  end

请不要忘记使用rails db:migrate从活动存储中迁移新生成的表。