Ruby on Rails: How are people making search forms on sites with the broken textfield parsing that removes quotation marks?

StackOverflow https://stackoverflow.com/questions/3704610

Question

I'm using Sunspot Solr search, which works fine for the most part for basic search. It's supposed to be able to handle quotation marks around phrases, so that a search for test case will return documents with both test and case, whereas a search for "test case" should return documents with the phrase test case.

However, I've been pulling my hair out over this, because it seems like Rails is stripping out any outer quotation marks from user inputs before it even hits the search engine. Thus "test case" returns the exact same results as test case but ""test case"" or "test case" (with leading and trailing spaces) actually works, in the first case because the outer quotation marks are stripped away leaving the inner ones, and in the second case because this problem only affects leading and trailing quotation marks.

Apparently, this is a known bug and has been marked as won't fix by the Rails team. I'm really surprised how little I can find on this online, since it seems like very common functionality.

How are people getting around this? It doesn't seem like a reasonable solution to me the ask the users to double quote things because of this, and I don't particularly want to go making my own custom modifications to Rack.

Was it helpful?

Solution 2

Hmm guess not many are running Rails apps that need searching that supports quotation marks?

I'm getting around this using the Rack patch linked to through the bug report for now until Rails fixes this bug.

Edit: Adding the links

Found on this page:
https://rails.lighthouseapp.com/projects/8994/tickets/4808

Direct download:
https://rails.lighthouseapp.com/projects/8994/tickets/4808/a/662679/fix_rack_110_quote_parsing.rb

However, this is definitely not perfect, and I am still finding some cases, such as ending with a trailing quote cause the leading part to be truncated.

OTHER TIPS

Apparently, from the aforementioned Lighthouse ticket, "If you add a new line character then double quotes [are] preserved." (Example.)

You might consider using Javascript to append a newline to your search string. It's a bit of a hack, but should get you past the Rack bug with no adverse effect on your queries.

A quick example off the top of my head in jQuery. Un-tested, ymmv, etc.

// append a newline to a field when submitting a form
// to work around a Rack parsing bug

$('#your_form').submit(function() {
  $('#your_input').val(function(i, val) {
    return val + "\n";
  };
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top