百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
C

chosen-rails

> 编程语言
开源

将选定的 JavaScript 库与 Rails 资产管道集成

521 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

将选定的 JavaScript 库与 Rails 资产管道集成

Chosen for rails asset pipeline

Chosen is a library for making long, unwieldy select boxes more user friendly.

The chosen-rails gem integrates the Chosen into Rails asset pipeline with the sprockets-rails.

Usage

Install chosen-rails gem

Include chosen-rails in Gemfile

rb
gem 'jquery-rails'
gem 'chosen-rails'

Then run bundle install

About JQuery

You can get jquery via jquery-rails.

Please consider jquery-turbolinks if you have turbolinks issues for Rails 4 +.

Include chosen javascript assets

Add to your app/assets/javascripts/application.js if use with jQuery

coffee
//= require jquery
//= require chosen-jquery

Or with Prototype

coffee
//= require jquery
//= require chosen-prototype

Include chosen stylesheet assets

Add to your app/assets/stylesheets/application.css

scss
*= require chosen

Enable chosen javascript by specific css class

For rails 6, remember to add javascript_include_tag in app/views/layouts/application.html.erb, like

ruby

Add to one coffee script file, like scaffold.js.coffee

coffee
$ ->
  # enable chosen js
  $('.chosen-select').chosen
    allow_single_deselect: true
    no_results_text: 'No results matched'
    width: '200px'

Notice: width option is required since Chosen 0.9.15.

And this file must be included in application.js

coffee
//= require chosen-jquery
//= require scaffold

Also add the class to your form field

erb

If you use simple form as form builder

erb

Deployment

Since version 0.13.0, non-digested assets of chosen-rails will simply be copied from digested assets.

RSpec helpers

chosen-rails provides RSpec feature helper methods that allow users to select or unselect options from both single and multiple chosen elements. Add the following to your spec/rails_helper.rb (or spec/spec_helper.rb):

ruby
require 'chosen-rails/rspec'

This automatically configures RSpec by adding:

ruby
RSpec.configure do |config|
  config.include Chosen::Rspec::FeatureHelpers, type: :feature
end

Configuration includes two additional methods for all type: :feature specs:

ruby
chosen_select(value, options = {})
chosen_unselect(value, options = {})

Both methods require from: '...' inside the options hash that is either a CSS selector or a field's label (requires the for attribute on the label!).

Example usage

To handle a single select:

ruby
chosen_select('Leo Tolstoy', from: 'Author')
chosen_unselect('Leo Tolstoy', from: '#author')
chosen_select('Fyodor Dostoyevsky', from: '#author')

To handle a multiple select:

ruby
chosen_select('Leo Tolstoy', 'Fyodor Dostoyevsky', 'Anton Chekhov', from: 'Authors')
# or, by single value:
chosen_select('Leo Tolstoy', from: '#authors')

chosen_unselect('Fyodor Dostoyevsky', ' Anton Chekhov', from: 'Authors')
# or, by single value:
chosen_unselect('Leo Tolstoy', from: '#authors')

Gem maintenance

Maintain chosen-rails gem with Rake commands.

Update origin chosen source files.

bash
rake update-chosen

Publish gem.

bash
rake release

License

use MIT license.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言