Select2 has a good feature in helping users to search record from a dropdown select option.
However, we should optimize the record by having ajax loading from the database. If we load all the record from the database, it will lead to slow loading and potentially timeout issue.
- When user visit the page, it will not load the select2 option.
- Till the user search from the select2 then only it will trigger an ajax callback to populate user the option with the search result.
2. The controller callback for select2 search result
3. Input form html to display the select2
<%= f.hidden_field :name, :class=>"select2", :id=>"item_select2", :"data-url" => items_path(:format => :json) %>