【问题标题】:select2 not displaying in Rails 3.2.8在 Rails 3.2.8 中未显示 select2
【发布时间】:2012-08-16 03:34:03
【问题描述】:

按照此处的说明进行操作:

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

我仍然无法在我的简单 Rails 应用程序中显示 select2。我已确保已安装 gem,将 //= require select2 添加到 application.js,并将 *= require select2 添加到 application.css。此外,我在users.js.coffee 中包含以下内容:

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

我有一个 User 模型,它有很多 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

我正在尝试让 select2 显示在我的 users/index.html.erb 页面上的 collection_select 上:

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

因此我的 javascripts 中的 $('#user_position_ids').select2 行。由于某种原因无法显示 select2,因此非常感谢有人指出我正确的方向。谢谢!

【问题讨论】:

    标签: jquery ruby-on-rails jquery-select2 select2-rails


    【解决方案1】:

    我遇到了同样的问题。将括号添加到我的咖啡脚本后,它可以正常工作。

    所以,在你的情况下:

    user.js.coffee:

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

    或有一些选项:

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

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 2012-09-17
      • 1970-01-01
      • 2012-09-05
      • 1970-01-01
      相关资源
      最近更新 更多