【问题标题】:Rails selectize Gem installationRails 选择 Gem 安装
【发布时间】:2015-04-22 13:49:08
【问题描述】:

尝试添加选择

来源:http://brianreavis.github.io/selectize.js/

宝石来源:https://github.com/manuelvanrijn/selectize-rails

添加到 Gemfile:

gem "selectize-rails"

添加了 application.js

//= require selectize

添加到 application.css

 *= require selectize
 *= require selectize.default

我的表格:

  <%= f.select :user, User.not_god.map{|u| [u.to_s, u.id]}, { id: 'user-select' } %>

我的 JS:

    $('#user-select').selectize({ <-- Uncaught 
                                  TypeError: 
                                  undefined is not a function
      create: true,
      sortField: 'text'
    });

这是给我Uncaught TypeError: undefined is not a function

有人用 selectize 遇到过这个问题吗?

非常感谢!

【问题讨论】:

    标签: javascript jquery ruby-on-rails selectize.js


    【解决方案1】:

    您在 options 哈希内分配 HTML id 属性;您想在 html_options 哈希中分配它。尝试为options 传递一个空哈希:

    <%= f.select(
      :user, 
      User.not_god.map{|u| [u.to_s, u.id]}, 
      {}, 
      { id: 'user-select' } 
     ) %>
    

    查看文档以了解形式参数的顺序。

    http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html#method-i-select

    编辑:

    此外,请检查您的 jQueryselectize 和自定义脚本是否以正确的顺序加载。

    app/assets/javascripts/application.js

    //= require jquery
    //= require jquery_ujs
    //= require selectize
    
    $(function() {
      $('#user-select').selectize({
        create: true,
        sortField: 'text'
      });
    });
    

    【讨论】:

    • 感谢您指出这一点,知道为什么 JS 会输出错误吗?
    • 另外,你使用的是什么版本的selectize-rails
    • 在 selectize-rails (0.12.0) 上,正确的顺序是什么? jQuery第一?然后选择?
    • 更新了答案。是的,那个命令。
    • ` //= 需要 jquery //= 需要 jquery_ujs //= 需要 jquery-ui //= 需要 selectize ` 这就是我所拥有的
    猜你喜欢
    • 2013-01-19
    • 2011-09-04
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2016-05-02
    • 2019-09-09
    • 2011-04-08
    • 2023-03-19
    相关资源
    最近更新 更多