Skip to content

Commit

Permalink
Fix links to absolute URLs
Browse files Browse the repository at this point in the history
govuk_url_for is used in three places in this codebase, all in view code
like:

      <%= link_to short_url_request.to_path, govuk_url_for(short_url_request.to_path) %>

`to_path` can be an absolute URL though, and in this case the
application will render a link like:

     <a href="http://gov.ukhttp://example.com">http://example.com</a>

which is real clown shoes stuff.

Addressable's URI.join method basically does what we want - if we're
given a path, it will join that on to the website root, if we're given
an absolute URL it will give us that.
  • Loading branch information
richardTowers committed Feb 23, 2024
1 parent 4d630fa commit 8f0240b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/helpers/url_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module UrlHelper
def govuk_url_for(path)
(Plek.website_root + path).to_s
Addressable::URI.join(Plek.website_root, path).to_s
end

def short_url_manger_url_for(path)
Expand Down
4 changes: 4 additions & 0 deletions spec/helpers/url_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
it "should return a fully qualified gov.uk url from the given path" do
expect(govuk_url_for("/some/path")).to eql "http://www.dev.gov.uk/some/path"
end

it "should leave absolute URLs unchanged" do
expect(govuk_url_for("http://example.com/some/other/path")).to eql "http://example.com/some/other/path"
end
end

describe "short_url_manger_url_for" do
Expand Down

0 comments on commit 8f0240b

Please sign in to comment.