Go back to the main page

Using javascript to fix missing double quotes in Rails

 

Search fields broken everywhere

Submitting a doubled quoted search phrase broke in Simplton after upgrading to Rail 2.3.10. Basically the quotes get striped out in the params object. Rob Anderton has a nice write up detailing the problem and providing a patch. However, you can also do a very hacky javascript work around. Being that if a space or some other character is a added to the end of the input value then the double quotes stay intact.

Here is the search input HTML for Simplton.


    <form action="/list/jenexpress/search" method="post" class="searchForm">
      <input class="searchField" id="search" name="search" size="50" value="" type="text">
      <button id="searchButton">Search</button> 
    </form>

Here is the javascript that adds a space to the end of the search input value. It is in Prototype.js but sub in your js lib. Note that you don't have to stop the submit event just modify the search input value before it gets sent off to Rack.

     document.observe( 'dom:loaded',function(){
        $$('.searchForm').invoke('observe','submit',function(){ 
          var inp = this.down('input');
          if(!inp.value.endsWith(' ')){ // don't keep adding space if there is no need
            inp.value += ' ';
          }
        });
     });
  • Pushed on 11/10/2010 by Christian