Отсутствует хост для ссылки на!с ActiveStorage с полем файла direct_upload в стандартном представлении - PullRequest
0 голосов
/ 22 сентября 2019

У меня есть приложение на Rails 6, которое содержит множество веб-сайтов и поэтому должно работать с несколькими доменами в производственном процессе.

<%= f.file_field :media, direct_upload: true %>

Когда я использую указанное выше поле файла с direct_upload: true в производственном процессе, я получаю:

ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

Все операции ActionView / ActionText работают без проблем в разработке, но когда я публикую в производство (Heroku), «host» становится неопределенным и выдает исключение всякий раз, когда вызывается маршрут ActiveStorage, такой как rails_direct_uploads_url.

В процессе разработки rails_direct_uploads_url генерирует ожидаемый URL с текущим хостом (myapp.test), не требуя каких-либо дополнительных определений хоста.В производственной среде хост должен быть равен нулю, так как генерируется исключение.

Если я определяю хост по умолчанию в Маршрутах, он захватывает это, но затем возникает несоответствие хоста - он использует хост по умолчанию, а не текущий хости портит кучу вещей.

Конфигурация системы

Версия Rails: 6.0.0 Версия Ruby: 2.5.5

Вот трассировка стека:

ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
  from action_dispatch/http/url.rb:63:in `full_url_for'
  from action_dispatch/http/url.rb:53:in `url_for'
  from action_dispatch/routing/route_set.rb:336:in `block in <class:RouteSet>'
  from action_dispatch/routing/route_set.rb:211:in `call'
  from action_dispatch/routing/route_set.rb:329:in `block in define_url_helper'
  from action_view/helpers/form_tag_helper.rb:914:in `convert_direct_upload_option_to_url'
  from action_view/helpers/form_helper.rb:1213:in `file_field'
  from action_view/helpers/form_helper.rb:2397:in `file_field'
  from app/views/items/new.html.erb:14:in `block in _app_views_items_new_html_erb__836175131764892061_70035750244440'
  from action_view/helpers/capture_helper.rb:45:in `block in capture'
  from action_view/helpers/capture_helper.rb:209:in `with_output_buffer'
  from haml/helpers/action_view_xss_mods.rb:7:in `with_output_buffer_with_haml_xss'
  from action_view/helpers/capture_helper.rb:45:in `capture'
  from haml/helpers/action_view_mods.rb:48:in `capture_with_haml'
  from action_view/helpers/form_helper.rb:755:in `form_with'
  from app/views/items/new.html.erb:5:in `_app_views_items_new_html_erb__836175131764892061_70035750244440'
  from action_view/base.rb:274:in `_run'
  from action_view/template.rb:185:in `block in render'
  from active_support/notifications.rb:182:in `instrument'
  from action_view/template.rb:386:in `instrument_render_template'
  from action_view/template.rb:183:in `render'
  from action_view/renderer/template_renderer.rb:59:in `block (2 levels) in render_template'
  from action_view/renderer/abstract_renderer.rb:89:in `block in instrument'
  from active_support/notifications.rb:180:in `block in instrument'
  from active_support/notifications/instrumenter.rb:24:in `instrument'
  from active_support/notifications.rb:180:in `instrument'
  from action_view/renderer/abstract_renderer.rb:88:in `instrument'
  from action_view/renderer/template_renderer.rb:58:in `block in render_template'
  from action_view/renderer/template_renderer.rb:66:in `render_with_layout'
  from action_view/renderer/template_renderer.rb:57:in `render_template'
  from action_view/renderer/template_renderer.rb:13:in `render'
  from action_view/renderer/renderer.rb:61:in `render_template_to_object'
  from action_view/renderer/renderer.rb:29:in `render_to_object'
  from action_view/rendering.rb:118:in `block in _render_template'
  from action_view/base.rb:304:in `in_rendering_context'
  from action_view/rendering.rb:117:in `_render_template'
  from action_controller/metal/streaming.rb:219:in `_render_template'
  from action_view/rendering.rb:103:in `render_to_body'
  from action_controller/metal/rendering.rb:52:in `render_to_body'
  from action_controller/metal/renderers.rb:142:in `render_to_body'
  from abstract_controller/rendering.rb:25:in `render'
  from action_controller/metal/rendering.rb:36:in `render'
  from action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
  from active_support/core_ext/benchmark.rb:14:in `block in ms'
  from benchmark.rb:308:in `realtime'
  from active_support/core_ext/benchmark.rb:14:in `ms'
  from action_controller/metal/instrumentation.rb:44:in `block in render'
  from action_controller/metal/instrumentation.rb:85:in `cleanup_view_runtime'
  from active_record/railties/controller_runtime.rb:34:in `cleanup_view_runtime'
  from action_controller/metal/instrumentation.rb:43:in `render'
  from action_controller/metal/implicit_render.rb:35:in `default_render'
  from action_controller/metal/basic_implicit_render.rb:6:in `block in send_action'
  from action_controller/metal/basic_implicit_render.rb:6:in `tap'
  from action_controller/metal/basic_implicit_render.rb:6:in `send_action'
  from abstract_controller/base.rb:196:in `process_action'
  from action_controller/metal/rendering.rb:30:in `process_action'
  from abstract_controller/callbacks.rb:42:in `block in process_action'
  from active_support/callbacks.rb:112:in `block in run_callbacks'
  from raven/integrations/rails/controller_transaction.rb:7:in `block in included'
  from active_support/callbacks.rb:121:in `instance_exec'
  from active_support/callbacks.rb:121:in `block in run_callbacks'
  from active_support/callbacks.rb:139:in `run_callbacks'
  from abstract_controller/callbacks.rb:41:in `process_action'
  from action_controller/metal/rescue.rb:22:in `process_action'
  from action_controller/metal/instrumentation.rb:33:in `block in process_action'
  from active_support/notifications.rb:180:in `block in instrument'
  from active_support/notifications/instrumenter.rb:24:in `instrument'
  from active_support/notifications.rb:180:in `instrument'
  from action_controller/metal/instrumentation.rb:32:in `process_action'
  from action_controller/metal/params_wrapper.rb:245:in `process_action'
  from active_record/railties/controller_runtime.rb:27:in `process_action'
  from abstract_controller/base.rb:136:in `process'
  from action_view/rendering.rb:39:in `process'
  from action_controller/metal.rb:191:in `dispatch'
  from action_controller/metal.rb:252:in `dispatch'
  from action_dispatch/routing/route_set.rb:51:in `dispatch'
  from action_dispatch/routing/route_set.rb:33:in `serve'
  from action_dispatch/journey/router.rb:49:in `block in serve'
  from action_dispatch/journey/router.rb:32:in `each'
  from action_dispatch/journey/router.rb:32:in `serve'
  from action_dispatch/routing/route_set.rb:837:in `call'
  from rack/deflater.rb:34:in `call'
  from rack/tempfile_reaper.rb:15:in `call'
  from rack/etag.rb:25:in `call'
  from rack/conditional_get.rb:25:in `call'
  from rack/head.rb:12:in `call'
  from action_dispatch/http/content_security_policy.rb:18:in `call'
  from rack/session/abstract/id.rb:232:in `context'
  from rack/session/abstract/id.rb:226:in `call'
  from action_dispatch/middleware/cookies.rb:648:in `call'
  from action_dispatch/middleware/callbacks.rb:27:in `block in call'
  from active_support/callbacks.rb:101:in `run_callbacks'
  from action_dispatch/middleware/callbacks.rb:26:in `call'
  from action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
  from action_dispatch/middleware/debug_exceptions.rb:32:in `call'
  from action_dispatch/middleware/show_exceptions.rb:33:in `call'
  from rails/rack/logger.rb:38:in `call_app'
  from rails/rack/logger.rb:26:in `block in call'
  from active_support/tagged_logging.rb:80:in `block in tagged'
  from active_support/tagged_logging.rb:28:in `tagged'
  from active_support/tagged_logging.rb:80:in `tagged'
  from rails/rack/logger.rb:26:in `call'
  from action_dispatch/middleware/remote_ip.rb:81:in `call'
  from action_dispatch/middleware/request_id.rb:27:in `call'
  from rack/method_override.rb:22:in `call'
  from rack/runtime.rb:22:in `call'
  from active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
  from action_dispatch/middleware/executor.rb:14:in `call'
  from action_dispatch/middleware/static.rb:126:in `call'
  from rack/sendfile.rb:111:in `call'
  from action_dispatch/middleware/ssl.rb:74:in `call'
  from action_dispatch/middleware/host_authorization.rb:77:in `call'
  from raven/integrations/rack.rb:51:in `call'
  from rack/cors.rb:95:in `call'
  from rails/engine.rb:526:in `call'
  from rails/railtie.rb:190:in `public_send'
  from rails/railtie.rb:190:in `method_missing'
  from puma/configuration.rb:228:in `call'
  from puma/server.rb:664:in `handle_request'
  from puma/server.rb:467:in `process_client'
  from puma/server.rb:328:in `block in run'
  from puma/thread_pool.rb:135:in `block in spawn_thread'

Я искал похожие проблемы и уже добавил в ApplicationController следующее, и он ничего не исправил:

class ApplicationController < ActionController::Base

  def default_url_options(options = {})
    { host: request.host }
  end

  before_action do
    ActiveStorage::Current.host = request.base_url
  end
end

Как указать хост при каждом запросе ИЛИ предоставить егоfile_field, rich_text_area и т. д., чтобы он не генерировал упомянутое исключение?

...