1

I have a search field in a Rails App. It is implemented this way:

<%= text_field_tag :searchAny, params[:searchAny_params], placeholder: "Search", id: "search_input", class: "search-field" %>
<%= submit_tag "Search", name: nil, id: "search-button", class: "search-button" %>

When I search for strings, which contain spaces such as: find something, I get find something in params, which is then rendered in the url as find+something. To my knowledge spaces used to be escaped through +, but now they should be escaped through %20.

I very much would like to switch to %20 in my url, because I access the query string in my frontend in order to make it available to new search functions or parse it somewhere and would need the + sign as a viable query string. Currently I am unable to search for house+ or +, because the JS function I wrote splits the search string and currently has to replace + with space.

var searchquery = searchParams.split('?').pop().split('&').pop().split('=')[1].replace(/[+]/g, ' ');

After my refactoring I hope to get the search string part of my url with %20 for space and leave + available.

var searchquery = searchParams.split('?').pop().split('&').pop().split('=')[1].replace(/%20/, ' ');

How can I change how params are escaped in the search string?

I hope someone can put me in the right direction. Thanks in advance.

Addition

Currently my Rails app produces the following URL:

https://www.domain.tld/collection/opac?searchAny=find+something

I would prefer the query string to be already % escaped:

https://www.domain.tld/collection/opac?searchAny=find%20something
SEJU
  • 995
  • 1
  • 9
  • 28
  • 2
    Don't reinvent the wheel. Use [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams) to parse the query string. It will properly decode all characters. – Stefan Jun 13 '22 at 12:06

1 Answers1

2

Based on this answer from a similar question, you can use the Addressable gem. Or alternatively you can try using Javascript's encodeUri() to handle the URL encoding from the front-end side. For example:

document.getElementById('search_button').addEventListener('click', function() {
    var searchInput = document.getElementById("search_input");
    var encodedQuery = encodeURI(searchInput.value)
    // continue to use the encodedQuery as search query
});

Hope this helps!

  • Thanks for you suggestion. The Addressable gem sounds promising. How can I implement it in order to configure how params are escaped in the url? – SEJU Jun 13 '22 at 19:24
  • If you simply want spaces to be escaped into %20 instead of +, you just need to use [Addressable::URI.encode()](https://rubydoc.info/gems/addressable/2.2.4/Addressable/URI#encode-class_method). – Stefano Christian Wiryana Jun 14 '22 at 00:57
  • Could you please give me a hint where I would have to put it? I thought about it, but have no idea where. – SEJU Jun 14 '22 at 03:16
  • I tried the JS way, but it already escapes the params I send to the backend for my database queries. I am looking for a way, how to configure how params with spaces are encoded in the resulting URL, when the search result is displayed in the client. Usually the URL is produced by routes.rb. – SEJU Jun 14 '22 at 03:25
  • Existing encoding or escape functions already have a fixed rule on what escape character to use so you can't manually configure the params to tailor it for you specific need. – Stefano Christian Wiryana Jun 14 '22 at 15:54
  • Thanks for your feedback. I guess I could escape the request in the client and deescape it on the server side before making any database search queries. I will leave it for now the way it is. – SEJU Jun 15 '22 at 05:41