开发者

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

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 开发者_JS百科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.


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";
  };
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜