它不起作用 select2 ruby on rails 5 [英] It does not work select2 ruby on rails 5
问题描述
它不起作用 select2 没有给我带来任何价值,就像带来价值一样,我是编程新手,我想学习很多东西,感谢您的帮助
It does not work select2 does not bring me any value, like bring value, I'm new to programming and I want to learn a lot, thanks for the help
application.css.scss
*= require toastr
*= require jquery-ui
*= require select2
*= require select2-bootstrap
*= require_tree .
*= require_self
*/
@import 'bootstrap-sprockets';
@import 'bootstrap';
@import 'bootstrap-datetimepicker';
@import "select2";
@import "select2-bootstrap";
@import "font-awesome-sprockets";
@import "font-awesome";
application.js
//= require jquery
//= require turbolinks
//= require jquery_ujs
//= require jquery-ui/widgets/autocomplete
//= require autocomplete-rails
//= require select2-full
//= require bootstrap-sprockets
//= require moment
//= require moment/es
//= require bootstrap-datetimepicker
//= require toastr
//= require pickers
//= require_tree .
$( "#dropdown" ).select2({
theme: "bootstrap"
});
meetings.js
$(document).ready(function() {
$('#ajax-example').select2({
ajax: {
url: "/meetings.json",
dataType: "json",
results: function(data, page) {
return {
results: $.map( data, function(meeting, i) {
return { id: meeting.id, text: meeting.name }
} )
}
}
}
});
});
index.html.erb
**(索引电流)**
<input type="text" id="ajax-example" />
meetings_controller.rb
def index
@meetings = Meeting.order('name').all
end
Gemfile
gem 'bootswatch-rails'
gem 'simple_form'
gem 'bootstrap-sass', '~> 3.3', '>= 3.3.7'
gem 'jquery-rails'
gem 'will_paginate', github: 'jonatack/will_paginate'
gem 'ransack'
gem 'will_paginate-bootstrap'
gem "select2-rails"
推荐答案
将您的会议控制器更改为:
Change your meetings controller to this:
respond_to :html, :json
def index
@meetings = Meeting.order('name').all
respond_with @meetings
end
这样做是允许控制器使用会议模型的 JSON 数组进行响应.select2
插件需要它来获取将在选择菜单中显示的数据.
What that does is allow the controller to respond with a JSON array of you meetings models. The select2
plugin needs this to be able to fetch the data it will display in the select menu.
这是关于此主题的教程:http://blog.teamtreehouse.com/using-select2-with-ruby-on-rails-treehouse-quick-tip
Here is a tutorial on this topic: http://blog.teamtreehouse.com/using-select2-with-ruby-on-rails-treehouse-quick-tip
这篇关于它不起作用 select2 ruby on rails 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!