Patrick Barattin Patrick Barattin - 6 months ago 37x
Javascript Question

Select2, create empty collection in Ruby on Rails form

I need to create a Select2 empty collection, because I add element after other selection.
This is my HTML code:

<div class="form-group">
<label class="col-md-3 control-label">Tutor</label>
<div class="col-md-4">
<%= f.input_field :company_tutor_id,collection: , prompt: 'Tutor', class: 'form-control select2' %>

What I have to write after "collection:"?
If I don't insert the collection option I give this error:

uncaught exception: query function not defined for Select2 training_project_company_tutor_id

Or should I create it with jquery?

This is the function i call after the section of "contract" for for adding options in Select2:

function getFarm(contract) {
$farm = $.get(Routes.farm_path(contract.farm_id, {format: 'json'}), fillFarm).success(function getTutorAziendale(farm) {

var jqXHR = $.get(Routes.company_tutors_path({format: 'json'}));

jqXHR.complete(function (response) {
$company_tutors = response.responseJSON;

$.each($company_tutors, function (index, obj) {

console.log("il mio id: " +;

if (obj.farm_id === {


var tutor = $('<option />', {
text: obj.nome


Thanks for your help.


I have solved my problem in this way:

I add all the CompanyTutor:

<%= f.input_field :training_project_textId, collection: TrainingProjectText.all ,prompt: 'Scegli un Modulo...', class: 'form-control select2' %>

end I remove it in this way:

$(document).ready(function ()  $("#training_project_company_tutorId").html("");