Skip to content
This repository
Browse code

Fix asset_path in mounted engine

Historically serving assets from a mountable engine could be achieved by
running ActionDispatch::Static as a part of engine middleware stack or
to copy assets prefixed with an engine name. After introduction of
assets pipeline this is not needed as all of the assets are served or
compiled into main application's assets.

This commit removes the obsolete line making asset_path always generate
paths relative to the root or config.relative_url_root if it's set.

(closes #8119)
  • Loading branch information...
commit 445f14e97553de552745de26ebd5e49bc5c82f4e 1 parent 5984894
Piotr Sarnacki authored January 21, 2013
3  actionpack/lib/action_view/helpers/asset_url_helper.rb
@@ -132,8 +132,7 @@ def asset_path(source, options = {})
132 132
           source = compute_asset_path(source, options)
133 133
         end
134 134
 
135  
-        relative_url_root = (defined?(config.relative_url_root) && config.relative_url_root) ||
136  
-          (respond_to?(:request) && request.try(:script_name))
  135
+        relative_url_root = defined?(config.relative_url_root) && config.relative_url_root
137 136
         if relative_url_root
138 137
           source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/")
139 138
         end
9  railties/test/railties/mounted_engine_test.rb
@@ -89,6 +89,7 @@ class Engine < ::Rails::Engine
89 89
           get '/generate_application_route', to: 'posts#generate_application_route'
90 90
           get '/application_route_in_view', to: 'posts#application_route_in_view'
91 91
           get '/engine_polymorphic_path', to: 'posts#engine_polymorphic_path'
  92
+          get '/engine_asset_path', to: 'posts#engine_asset_path'
92 93
         end
93 94
       RUBY
94 95
 
@@ -113,6 +114,10 @@ def application_route_in_view
113 114
             def engine_polymorphic_path
114 115
               render text: polymorphic_path(Post.new)
115 116
             end
  117
+
  118
+            def engine_asset_path
  119
+              render inline: "<%= asset_path 'images/foo.png' %>"
  120
+            end
116 121
           end
117 122
         end
118 123
       RUBY
@@ -211,6 +216,10 @@ def app
211 216
       # and in an application
212 217
       get "/application_polymorphic_path"
213 218
       assert_equal "/posts/44", last_response.body
  219
+
  220
+      # test that asset path will not get script_name when generated in the engine
  221
+      get "/someone/blog/engine_asset_path"
  222
+      assert_equal "/images/foo.png", last_response.body
214 223
     end
215 224
 
216 225
     test "route path for controller action when engine is mounted at root" do

0 notes on commit 445f14e

Please sign in to comment.
Something went wrong with that request. Please try again.