Provide tag-helper methods for create Select2 elements by the same way as any other view elements.
Firstly install AutoSelect2 with dependency. Then add this line to your application's Gemfile:
gem 'auto_select2_tag'
And execute:
$ bundle
You can find example project here.
Gem provide only two helper methods for initialize select2 elements:
- select2_tag
- select2_ajax_tag
details about realization you can find on AutoSelect2 page.
select2_tag(name, option_tags = nil, options = {})
Has same behavior as common
select_tag. In addition
you can specify :select2_options
in options. This parameter allow to set select2
constructor options.
= select2_tag :select2_name,
my_options_for_select2(my_init_value),
placeholder: 'Fill me!',
include_blank: true,
select2_options: {width: 'auto'}
select2_ajax_tag(name, select2_searcher, value = nil, options = {})
All allowed options for this helper can be found on AutoSelect2 page. look at example below; this is an easy way to understand helper parameters.
If you define SearchAdapter
named MySearchAdapterName
, you can use the follow code:
= select2_ajax_tag :my_select2_name,
:my_search_adapter_name,
my_init_value_id,
placeholder: 'Fill me now!',
search_method: :unusual_case,
class: 'my-select2-input',
select2_options: {additional_ajax_data: {selector: '.additional-elements'}}
If you want lightweight selection over columns of ActiveRecord
model:
= select2_ajax_tag :my_select2_name,
{class_name: :your_model_name, text_column: :name, id_column: :id},
my_init_value_id,
placeholder: 'Fill me now!'
In this case you get select2 selent with search by name
column over model YourModelName
.
- Fork it ( http://github.com/Loriowar/auto_select2_tag/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request