From 83613ca60b1d536003a58839cd878a0fd8673be3 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sun, 10 Mar 2013 12:47:32 +0100 Subject: [PATCH] `asset_path` respects SCRIPT_NAME. Closes #2992 --- actionpack/CHANGELOG.md | 2 ++ actionpack/lib/action_view/helpers/asset_url_helper.rb | 2 ++ actionpack/test/template/asset_tag_helper_test.rb | 10 ++++++++++ 3 files changed, 14 insertions(+) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 157a038b7c2bd..da961c7ef9629 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,7 @@ ## Rails 4.0.0 (unreleased) ## +* `asset_path` respects SCRIPT_NAME. Fixes #2992. *Yves Senn* + * Fix incorrectly appended square brackets to a multiple select box if an explicit name has been given and it already ends with "[]" diff --git a/actionpack/lib/action_view/helpers/asset_url_helper.rb b/actionpack/lib/action_view/helpers/asset_url_helper.rb index 71b78cf0b54ed..364685c857d4e 100644 --- a/actionpack/lib/action_view/helpers/asset_url_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_url_helper.rb @@ -133,6 +133,8 @@ def asset_path(source, options = {}) end relative_url_root = defined?(config.relative_url_root) && config.relative_url_root + request = self.request if respond_to?(:request) + relative_url_root ||= request.script_name if request if relative_url_root source = "#{relative_url_root}#{source}" unless source.starts_with?("#{relative_url_root}/") end diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index 00c860b91bdc6..5d5d4ce167014 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -733,6 +733,16 @@ def request assert_equal "http://www.example.com/foo", @module.asset_url("foo") end + def test_asset_url_with_script_name + @module.instance_eval do + def request + Struct.new(:base_url, :script_name).new("http://www.example.com", "/app") + end + end + + assert_equal "http://www.example.com/app/foo", @module.asset_url("foo") + end + def test_asset_url_with_config_asset_host @module.instance_eval do def config