简体   繁体   中英

select2 not displaying in Rails 3.2.8

Following the instructions here:

http://rubydoc.info/gems/select2-rails/3.1.0/frames

I still cannot get the select2 displays to show in my simple Rails app. I've made sure the gem installed, added //= require select2 to application.js , and added *= require select2 to application.css . Further, I've included the following in a users.js.coffee :

jQuery ->
  $('#user_position_ids').select2

I have a User model that has many Positions :

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, 
                  :first_name, :last_name, :position_ids

  has_many :positions
  has_one :current_position, :class_name => "Position", 
          :conditions => { :is_current => true }
end

I am trying to get the select2 to show on a collection_select on my users/index.html.erb page:

<%= form_for(@user) do |f| %>
  <%= f.collection_select(:position_ids, Position.order(:industry), :id, 
         :industry, {include_blank:true}, {multiple:true}) %>
<% end %>

Hence the $('#user_position_ids').select2 line in my javascripts. Just can't get select2 to display for some reason so would really appreciate someone pointing me in the right direction. Thanks!

I ran the same issue. After adding the brackets to my coffeescript it worked properly.

So, in your case:

user.js.coffee:

jQuery ->
  $('#user_position_ids').select2()

or with some options:

jQuery ->
    $('#user_position_ids').select2({
        placeholder: "Tags...",
        width: "element",

    })

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