title: Canonical way to insert extension into URL
body: <p>I have a URL in a Rails application that can look like a simple <code>/resource</code> or a more complex <code>/resource?page=3&amp;sort=id&amp;direction=asc</code>. The data behind the view can be exported by appending <code>.tsv</code> to the resource name - <code>/resource.tsv?page=3&amp;sort=id&amp;direction=asc</code>. To do this, I use this call in my <code>.erb</code>: <code>add_extension_to_url(request.original_url, '.tsv')</code> where I have:</p>
  
  <pre><code>module ResourceHelper
    def add_extension_to_url(url, extension)
      query_start = url.index('?')
      if query_start.present?
        url.insert(query_start, extension)
      else
        url + extension
      end
    end
  end
  </code></pre>
  
  <p>It works, but it's not pretty. Am I missing something like <code>request.query_string</code> that gives me what comes after the question mark in the URL?</p>