Skip to content

Commit 211c3f6

Browse files
authored
Merge pull request rails#45992 from pinzonjulian/pinzonjulian/allow_activestorage_direct_uploads_to_work_with_engines
allow direct uploads to work within engines
2 parents 4328d0e + f4812bf commit 211c3f6

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

actionview/lib/action_view/helpers/form_tag_helper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,9 +1013,14 @@ def set_default_disable_with(value, tag_options)
10131013
end
10141014

10151015
def convert_direct_upload_option_to_url(options)
1016-
if options.delete(:direct_upload) && respond_to?(:rails_direct_uploads_url)
1016+
return options unless options.delete(:direct_upload)
1017+
1018+
if respond_to?(:rails_direct_uploads_url)
10171019
options["data-direct-upload-url"] = rails_direct_uploads_url
1020+
elsif respond_to?(:main_app) && main_app.respond_to?(:rails_direct_uploads_url)
1021+
options["data-direct-upload-url"] = main_app.rails_direct_uploads_url
10181022
end
1023+
10191024
options
10201025
end
10211026
end

railties/test/railties/mounted_engine_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class Engine < ::Rails::Engine
118118
get '/application_route_in_view', to: 'posts#application_route_in_view'
119119
get '/engine_polymorphic_path', to: 'posts#engine_polymorphic_path'
120120
get '/engine_asset_path', to: 'posts#engine_asset_path'
121+
get '/file_field_with_direct_upload_path', to: 'posts#file_field_with_direct_upload_path'
121122
end
122123
RUBY
123124

@@ -150,6 +151,10 @@ def engine_polymorphic_path
150151
def engine_asset_path
151152
render inline: "<%= asset_path 'images/foo.png', skip_pipeline: true %>"
152153
end
154+
155+
def file_field_with_direct_upload_path
156+
render inline: "<%= file_field_tag :image, direct_upload: true %>"
157+
end
153158
end
154159
end
155160
RUBY
@@ -268,6 +273,10 @@ def app
268273
# test that asset path will not get script_name when generated in the engine
269274
get "/someone/blog/engine_asset_path"
270275
assert_equal "/images/foo.png", last_response.body
276+
277+
# test that the active storage direct upload URL is added to a file field that explicitly requires it within en engine's view code
278+
get "/someone/blog/file_field_with_direct_upload_path"
279+
assert_equal "<input type=\"file\" name=\"image\" id=\"image\" data-direct-upload-url=\"http://example.org/rails/active_storage/direct_uploads\" />", last_response.body
271280
end
272281

273282
test "route path for controller action when engine is mounted at root" do

0 commit comments

Comments
 (0)