1

I have a partial to load when a search is detected. It may take a very long time to load in some cases, so I would like it to load via Ajax once the page is fully loaded otherwise it will most likely cause a timeout.

The way I am doing it currently is resulting in a synchronously load.

I would also like a loading screen to be shown, until it's fully loaded. This is working currently but only with the timeout set, as its a synchronous load.

    <% if(params[:q][:name_cont].present?) %>

  <script>
    $( document ).ready(function() {

      function load_partial() {     
        $("div.super_search").replaceWith('<%= escape_javascript(render partial: 'super_search_tp') %>');
                                          }

                                          // use setTimeout() to execute
                                          setTimeout(load_partial, 1000)

      });

  </script>

  <div class="super_search">

    <i class='fa fa-spinner fa-spin'></i> Loading Product Analysis...

  </div>

<% end %>

1 Answer 1

1

Create a method for the partial in the controller:

  def super_search
    @search = @@products
    render :layout => false
  end

Rename the partial so it was the view for this method.

Add in the @@products to the index method so that the data from the search can be used in the new method:

@@top_products = @q.result

Code to render the partial:

<% if(params[:q][:name_cont].present?) %>

      <script>

        $.ajax({
          url: "/products/super_search",
          cache: false,
          success: function(html){
            $("#foo-bar").replaceWith(html);
          }
        });

      </script>

      <div class="super_search" id="foo-bar">

        <i class='fa fa-spinner fa-spin'></i> Loading Product Analysis...

      </div>

        <% end %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.