Rails + проблема ActiveStorage при запуске сервера = неопределенный метод has_one_attached - PullRequest
0 голосов
/ 21 ноября 2018

У меня проблема с конкретной моделью.Когда я запускаю свой сервер 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 голосов
/ 21 ноября 2018

Запустите rails active_storage:install и добавьте в файл config / environment / 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.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...