Я решил эту задачу с помощью select2+ajax, спасибо, что указали направление.
rails g scaffold City text:string
rails g scaffold User name:string
bundle install
Gemfilegem 'rails', '4.1.5'
gem 'select2-rails'
gem 'mysql2'
application.jsvar ready;
ready = function() {
$('#ajax-example').select2({
minimumInputLength : 0,
ajax: {
url: "/cities.json",
dataType: "json",
quietMillis: 100,
data: function(term) { return { q: term }},
results: function(data) {
return {
results: $.map( data, function(city, i) {
return { id: city.id, text: city.text }
} )
}
}
},
initSelection: function(element, callback) {
var id=$(element).val();
if (id!=="") {
$.ajax("/cities/"+id+".json").done(function(data) {
data ={ id: data.id, text: data.text }
callback(data);
})
}
}
});
};
$(document).ready(ready); //для работы с turbolinks
$(document).on('page:load', ready);
cities_controller.rbclass CitiesController < ApplicationController
def index
@cities = City.order('text').finder(params[:q])
respond_with @cities
end
end
views/users/_form.html.erb<div class="field">
<%= f.label :city %><br>
<%= f.hidden_field :city_id, :value =>@user.city_id, id: 'ajax-example', data: { source: cities_path } %>
</div>