jqueryruby-on-railsgeocomplete

Uncaught TypeError: Cannot read property 'cities' of undefined [Geocomplete]


I am using this plugin in Rails but still have a problem with error in title.

Definition of script:

script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&sensor=false>

Input text field:

<%= f.text_field :place, id: "cities", class: "form-control cities", type: "text" %>

Javascript load:

$("cities").geocomplete();
$.fn.geocomplete("cities"); 

I have done everything as described on GitHub but still cannot get it works. If I use $("#cities").geocomplete(); the error is the same.

Please, can somebody help me?


Solution

  • You should also add the jquery.geocomplete.js script inorder to make the geo-complete work.

    Scripts:

    <script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <script src="jquery.geocomplete.js"></script>
    

    You should load these two scripts, then call the function.

    $("#cities").geocomplete();  // Option 1: Call on element.
    

    Here is the reference