Pregunta

I ran the following code locally, where :query is "new york" and excerpt string contained "New York":

<%= excerpt.gsub(params[:query].to_s, '<strong><span style="background-color: yellow">\0</span></strong>').html_safe %>

Then gsub matched "New York". In other words, the gsub was case-insensitive. However, on Heroku, the match only took place if it matched exactly, i.e. "new york" is not assimilated with "New York".

I changed the code above code to:

<%= excerpt.gsub(/#{params[:query].to_s}/i, '<strong><span style="background-color: yellow">\0</span></strong>').html_safe %>

and now it's working both locally and on Heroku.

Previously I've run into differences between localhost and a Heroku deployment due to the database format: I use SQLite locally and Postgres on Heroku, where the case sensitivity rules are different for queries. But as this is just performing a gsub on a string variable, I can't figure out why the local instance would find a match when the server one wouldn't. Am I missing something here?

¿Fue útil?

Solución

If you pass a string to gsub as the first argument, it will only match that exact string. There is no notion of case insensitive match for a string. The behavior on Heroku is the right behavior. It is rather your local environment that is somehow messed up.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top