Я использую драгоценный камень select2 jquery. Я пытаюсь получить список тем, соответствующих типу пользователя. Он работает не так, как ожидалось, и я весь день пытался выяснить, почему. Я получаю эту ошибку в консолиRails select2 ajax tags
Uncaught TypeError: Cannot call method 'toUpperCase' of undefined select2.js?body=1:363
Uncaught TypeError: Cannot call method 'localeCompare' of undefined
Вторые ссылки ошибки в файле JS на этой линии
return this.text.localeCompare(term) === 0;
Есть ли что-то я делаю не так? Спасибо заранее.
Мой маршрут:
get '/topic/topic_list' => 'topic#topic_list'
В мой контроллер Тема у меня есть:
def topic_list
@topics = Topic.order(:name)
respond_to do |format|
format.html
format.json { render json: @topics.where('name like ?', "%#{params[:q]}%") }
end
end
Topić файл расслоение плотной
$(function() {
$('#story_topic_list').select2({
tags: true,
width: '100%',
tokenSeparators: [","," "],
createSearchChoice: function(term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {
id: term,
text: term
};
}
},
multiple: true,
maximumSelectionSize: 5,
formatSelectionTooBig: function (limit) {
return 'You can only add 5 topics'
},
ajax: {
dataType: 'json',
url: '/topic/topic_list.json',
data: function (term, page) {
return { q: term };
},
results: function(data, page) {
return { results: data };
}
}
})
});
В моей форме я есть
<%= f.label :topic_list %>
<%= f.text_field :topic_list %>
Также /topic/topic_list.json возвращает это:
[{"id":2,"name":"app","added_by":null,"cover_id":null,"created_at":"2014-03-23T20:12:24.615+00:00","updated_at":"2014-03-23T20:12:24.615+00:00","count":1},{"id":5,"name":"app tech website","added_by":1,"cover_id":null,"created_at":"2014-03-23T20:23:33.754+00:00","updated_at":"2014-03-23T20:23:33.754+00:00","count":1},{"id":3,"name":"tech","added_by":null,"cover_id":null,"created_at":"2014-03-23T20:12:24.668+00:00","updated_at":"2014-03-23T20:12:24.668+00:00","count":1},{"id":4,"name":"website","added_by":null,"cover_id":null,"created_at":"2014-03-23T20:12:24.677+00:00","updated_at":"2014-03-23T20:12:24.677+00:00","count":1}]
Спасибо, но это все-таки дает эту ошибку :( – Skyalchemist
@Skyalchemist - обновлено – mccannf
И вам может понадобиться изменить 'this.text.localeCompare (term)' to 'this.name.localeCompare (term)', если вы все еще получаете ошибку localeCompare – mccannf