2

I thought this would be fairly easy, but I'm not finding any help by Googling.
I have a form (simple_form) with numerous inputs with select lists (collections) that are populated from the database, so many it is slowing down the initial page load. I thought I could speed it up by only populating those drop down lists as the user selects them using Ajax. Is there something built in like remote => true for the form itself? Can someone point me in the right direction?

EDIT: I found this SO question but I cannot figure out how to implement the answer.
Currently, my form looks like this;

= simple_form_for(@account)
  = f.input :account_number
  = f.input :area, collection: @areas 
  = f.submit nil, :class => 'btn btn-primary'

Based on the answer in the linked question, I should add something like this, but of course it is not working

= simple_form_for(@account)
  = f.input :account_number
  = f.input :area, collection: @areas, :input_html => {"data-remote" => true, "data-url" => "/my_areas", "data-type" => :json}      
  = f.submit nil, :class => 'btn btn-primary'

2 Answers 2

3
+50

I can think of two ways to go about this if you don't want to load the contents initially when the page loads. One way is to run a script after the DOM has loaded to change the options for the select tag and the other is to collect the options when you click on the drop-down on the select element. I might go for the first way because there wouldn't be latency when a user clicks on the select element--they wouldn't have to wait for the options to populate.

So you'd run a jQuery script on document ready that makes an AJAX call to a method in your controller, which then returns the collections you want, then you iterate through the select elements you want to change with JQuery scripts. It might look something like this.

# in view with the select options to be changed
$(document).ready(function() {
  $.get(change_selects_path, function(response) {
    $.each(response, function(args) {
      // code for each select element to be changed
      $('.class_of_select_element').html(<%= j options_from_collection_for_select(args) %>);
    });  
  });
)};

# in controller
def change_selects
  # make db calls and store in variables to feed to $.get request
end

Note that this not tested but should give you a good start towards a solution. For further info on the each loop, you can check out this documentation.

1
1

Not sure if this fits your exact use case (please clarify if not), but I also have a few collection selects that have a large amount of database rows behind them. I use the select2-rails gem to take care of this. Users can begin to type in the name and the relevant results will show up (it will also show a few initially if they don't type something).

Check it out here: https://github.com/argerim/select2-rails

Edit: For a cascading dropdown, I recommend this gem: https://github.com/ryanb/nested_form

4
  • Thanks for the reference. It looks like select2 is designed to help with searching through long lists. What I'm really looking for is a way to only query the database if the user clicks the dropdown.
    – SteveO7
    Commented Apr 23, 2014 at 12:36
  • 1
    Okay, no problem. So, you are looking for what's known as a cascading dropdown select, correct? Then I would check out this gem, nested_form: github.com/ryanb/nested_form (which version of Rails are you on?)
    – Rachel9494
    Commented Apr 23, 2014 at 13:46
  • 1
    I'm using Rails 4.1.0. Humm... I'm not really needing the cascading part (one select list dependent on a value of another), I really only need a way to populate the select dropdown list using a client side event. Right now I create constants in an initializer and send the list data on page load. Because of the volume of data for all the lists, it slows down the page load. Typically a user will only use one or two selects in an edit session, but I'm populating around 20. The nested_form gem looks similar to cocoon, which I am already using.
    – SteveO7
    Commented Apr 23, 2014 at 16:46
  • Ah, I think I understand now. I am sorry, about the only thing that I know of to try would be a multi-page form, and put only a few on each page. Here is an example: github.com/xoxco/jQuery-Multipage-Form but I understand if you are looking for something else!
    – Rachel9494
    Commented Apr 23, 2014 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.