游.程 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭
    • ¥15 2020长安杯与连接网探
    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么