I have a URL in a Rails application that can look like a simple /resource
or a more complex /resource?page=3&sort=id&direction=asc
. The data behind the view can be exported by appending .tsv
to the resource name - /resource.tsv?page=3&sort=id&direction=asc
. To do this, I use this call in my .erb
: add_extension_to_url(request.original_url, '.tsv')
where I have:
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
It works, but it's not pretty. Am I missing something like request.query_string
that gives me what comes after the question mark in the URL?