简体   繁体   中英

It does not work select2 ruby on rails 5

在此处输入图片说明

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

** (index current)**

  <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

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.

Here is a tutorial on this topic: http://blog.teamtreehouse.com/using-select2-with-ruby-on-rails-treehouse-quick-tip

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM