Ajax select2 with rails


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.


  1. When user visit the page, it will not load the select2 option.
  2. Till the user search from the select2 then only it will trigger an ajax callback to populate user the option with the search result.

Project Setup
1. Setup the ajax select2 javascript

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) %>

Contact us about our engineering and design services

Let's Talk

Contact Us