游.程 2017-01-30 09:18 采纳率: 0%
浏览 24

Ajax调用和Rails

I have to make an ajax call in my application. There is 3 different tabs (I use bootstrap tabs), and each tab call a request, and the different request are displaying in different tab-content. I create a loader which is display when I make the ajax call, for UI. But I have a problem, when I have clicked on a tab, the Ajax call is processing well, but when I click an other time on the same tab, the ajax call restart.

I juste want that data be loaded only one time, at the first request.

Here is the code :

show.html.erb (link for the call)

<li role="presentation" class="<%= 'active' if @school.is_subscribed? %>">
                  <%= link_to "Vérifiés (#{@count_verified})", verified_rating_path(school_id: @school.id), id: 'verified_link', 'data-toggle' => 'tab' , 'data-target' => '#verifie',  remote: true %>
                </li>

**The loader **

  <div class="rating_loader"><i class="icon-spinner icon-spin icon-large"></i> Chargement...</div>
              <script>
                var loader = $(".rating_loader");
                loader.hide();

                $(document).ajaxStart(function() {
                  loader.fadeIn('slow');
                }).ajaxComplete(function() {
                  loader.hide();
                });

              </script>

School_controller.rb (the method)

  def verified_rating
    @school = School.find(params[:school_id])
    @ratings = @school.ratings.desc(:created_at)
    respond_to do |format|
      format.js
    end
  end

verified_rating.js.erb

$('#verifie').html("<%= escape_javascript render(:partial => 'rating', collection: @ratings.where(:verified => true)) %>");
$('#verified_link').one('click', function(e){
  e.preventDefault();
  $('#verifie').addClass('active');
  $('#non_verifie').removeClass('active');
  $('#all_avis').removeClass('active');
});

Is there a way to kill the call after the data are loaded and keep them in the page ?

Thank !

  • 写回答

0条回答 默认 最新

    报告相同问题?