Rails / Jquery Autocomplete automatically completes the plugin for city, country

Im looking for a good jQuery plugin that can handle searching for the country the city belongs to (by type with autocomplete)

eg:

Typing "New Yor" 

If autocomplete

 New York, United States 

And save the city and country as

 :city :country 

What would be a good way to achieve this functionality? I mean more in terms of storing the entire country and city in a user database. I know https://github.com/jim/carmen , but this is being corresponded, and I would love to think about it, with a little help.

+6
source share
2 answers

You can also write your own logic for this, since I don't think the plugin is going to introduce itself:

I assume that there are identifiers in the city and country. If not, the logic will be simpler. Use jQueryUI autocomplete to run autocomplete for your element. Add a controller method that will give the label of your city and country (probably the method for the city object) along with your ids back as json. In the "select" completion callback, set hidden fields in the form for city and country identifiers. This last part is optional because I don’t know how you plan to save this data from the form.

The following examples assume a LOT about your application, but should be enough to get you started:

example script for view:

  $("#yourField").autocomplete({ source: "/path_to_your_action", minLength: 2, select: function( event, ui ) { $(this).val(ui.item.label); $(this).find("#country_id").val(ui.item.country_id); $(this).find("#city_id").val(ui.item.city_id); event.preventDefault; } }) 

controller:

 def your_action term = params[:term] cities = City.where("cities.name like ?", "%#{term}%") .limit(25) .all render :json=>cities.collect{|c| {:label=>c.your_label_method, :city_id=>c.id, :country_id=>c.country_id}} end 

city.rb

 def your_label_method "#{self.name}, #{self.country.name)}" end 
+8
source

In your case, the best solution for me is to use a google map api to find out a good income. You can, for example, use the htp plugin: //github.com/sgruhier/jquery-addresspicker.

0
source

Source: https://habr.com/ru/post/907234/


All Articles