如何解决AJAX检查框时,应时刻火? [英] How to fix AJAX to always fire when checking box?

查看:201
本文介绍了如何解决AJAX检查框时,应时刻火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,这AJAX火灾和放大器;有时不,让我解释一下。

habit.js

  $(文件)。就绪(函数()
{
  $(习惯检查)。改变(函数()
  {
    习惯= $(本).parent()兄弟(习惯-ID。)(第)ATTR(ID)。;
    级= $(本).siblings(电平-ID)的第一()ATTR(ID)。;
    如果($(本)。是(:选中))
    {
       $阿贾克斯(
       {
         网址:/习惯/+习惯+/级别/+等级+/ days_missed
         方法:POST
       });
    }
    其他
    {
       $阿贾克斯(
       {
         网址:/习惯/+习惯+/级别/+等级+/ days_missed / 1,
         方法:DELETE
       });
    }
  });
});
 

据后,才触发加载显示页面并刷新它,这样的终端是这样的:

 入门掌握在2015年6月20日二十三时55分42秒-0400/习惯/ 1:: 1
处理由HabitsController#显示为HTML
  参数:{ID=>中1}
  用户负载(0.1毫秒)选择用户。*从用户WHERE用户,ID=? LIMIT 1 [身份证,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  ActsAsTaggableOn ::标记加载(为0.2ms),选择标签。* FROM标签WHERE(LOWER(名称)= LOWER('根深蒂固'))
  CACHE(0.0ms)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  负载级(为0.2ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  ActsAsTaggableOn ::标记加载(0.3ms)SELECT DISTINCT标签。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。tagger_id= ?和的Tagging。tagger_type=? ORDER BY taggings_count递减LIMIT 20 [[tagger_id,1],[tagger_type,用户]
   (0.1毫秒)SELECT COUNT(*)FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。ID=? LIMIT 1 [身份证,1]
  CACHE(0.0ms)选择习惯。* FROM习惯WHERE习惯。ID=? LIMIT 1 [ID,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。user_ID的=?与习惯。ID=? LIMIT 1 [user_ID的,1],[身份证,1]
  CACHE(0.0ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  ActsAsTaggableOn ::标记加载(为0.2ms),选择标签。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。taggable_id=?和的Tagging。taggable_type=?与(taggings.context =标签)的GROUP BY tags.id [[taggable_id,1],[taggable_type,习惯]]
  注意负载(0.1毫秒)选择注意事项。* FROM注意事项WHERE注意事项。habit_id=? ORDER BY notes_date [habit_id,1]
  渲染票据/ _notes.html.erb(2.0ms)
  渲染票据/ _form.html.erb(3.9ms)
  用户负载(0.1毫秒)选择用户。*从用户INNER JOINhabit_likesON用户,ID=habit_likes。user_ID的WHEREhabit_likes。habit_id=? [habit_id,1]
  评论加载(0.1毫秒)选择注释。* FROM意见WHERE意见。habit_id=? [habit_id,1]
  渲染评论/ _comments.html.erb(为0.8ms)
  渲染评论/ _form.html.erb(1.0ms的)
  渲染习惯/布局内show.html.erb /应用程序(24.0ms)
   (为0.2ms)SELECT COUNT(*)FROM通知WHERE通知。user_ID的=? [user_ID的,1]
  渲染布局/ _header.html.erb(2.9ms)
   (0.1毫秒)SELECT COUNT(*)FROM习惯WHERE习惯。user_ID的=?与1 = 0 [[USER_ID,1]]
  渲染习惯/ _today.html.erb(1.0ms的)
  目标负荷(为0.2ms),选择目标。* FROM目标WHERE目标。user_ID的=?与目标,完成='F'ORDER BY期限LIMIT 3 [user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM用户INNER JOIN ON用户的关系,ID=关系。followed_idWHERE关系。follower_id=? [follower_id,1]
   (0.1毫秒)SELECT COUNT(*)FROM目标WHERE目标。user_ID的=?与目标,完成='F'[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROMquantifiedsWHEREquantifieds。user_ID的=? [user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM用户INNER JOIN ON用户的关系,ID=关系。follower_idWHERE关系。followed_id=? [followed_id,1]
   (0.1毫秒)SELECT COUNT(*)FROM目标WHERE目标。user_ID的=?与目标,完成=T[[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM结果INNER JOINquantifiedsON结果。quantified_id=quantifieds。IDWHEREquantifieds。user_ID的=?和结果,坏='F'[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM估值WHERE估值。user_ID的=? [user_ID的,1]
  渲染布局/ _count.html.erb(10.7ms)
  渲染布局/ _tags.html.erb(0.1毫秒)
  渲染布局/ _recommendations.html.erb(0.0ms)
  量化负荷(0.1毫秒)选择quantifieds。* FROMquantifiedsWHEREquantifieds。user_ID的=?与quantifieds。类别=? [user_ID的,1],[类,平均]
  量化负荷(0.1毫秒)选择quantifieds。* FROMquantifiedsWHEREquantifieds。user_ID的=?与quantifieds。类别=? [user_ID的,1],[类,实例]
   (0.1毫秒)SELECT COUNT(*)FROM估值WHERE估值。user_ID的=?与(1 = 0)[[USER_ID,1]]
  渲染布局/ _value.html.erb(为0.8ms)
  渲染布局/ _sidebar.html.erb(18.9ms)
完成200 OK在324ms(浏览次数:307.4ms | ActiveRecord的:3.3ms)


开始于2015年6月20日23点55分42秒-0400 GET/assets/application-2b528cffd402884831208f505b651927.css?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/jquery-ea2706bc2d0586731b7c00ec2cd0a414.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/stylesheet-3e834b3a950daa6165191fa42b13a968.css?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/jquery_ujs-29277b946c1d3754af13672bfec0c660.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/bootstrap-d16805677947f8c7ce91cea7ae7f82b0.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/cocoon-07a2b3765fb91baf2df1cea98d97eed0.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/turbolinks-3a7cdb8630f3e86770867bd488a098e0.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/account_activati​​ons-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/comments-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/days_missed-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/facebook-faaf7b74474e5a746a0c4a160e4705d2.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/habit-ff66f57b87d00b4469848f1af3f6910a.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/habits-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/activities-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/pages-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/password_resets-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/notes-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/quantified-5cae96afee8ab50e506c00828f3042b3.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/goals-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/quantifieds-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/relationships-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/sessions-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/notifications-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/tags-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/users-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/values​​-8f6eecd1178ef85964369ec66d33c4f1.js?body=1为:: 1


开始于2015年6月20日23点55分42秒-0400 GET/assets/application-6fd7d78d93af9f6901c08c3bc66303d0.js?body=1为:: 1


入门使用/assets/bootstrap/glyphicons-halflings-regular-195cb410b49d75c104a5bc6ad385ac77.woff为:: 1的二〇一五年六月二十零日二十三时55分43秒-0400
 

只是加载网页显示一次不会做的工作,因为对号不火的AJAX(注意:所有的GET以上和放大器;缺乏GET以下的,我认为这是与turbolinks)。

 入门掌握在2015年6月21日〇时09分02秒-0400/习惯/ 1:: 1
处理由HabitsController#显示为HTML
  参数:{ID=>中1}
  用户负载(为0.2ms),选择用户。*从用户WHERE用户,ID=? LIMIT 1 [身份证,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  ActsAsTaggableOn ::标记加载(为0.2ms),选择标签。* FROM标签WHERE(LOWER(名称)= LOWER('根深蒂固'))
  CACHE(0.0ms)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  负载级(为0.2ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  ActsAsTaggableOn ::标记加载(为0.2ms)SELECT DISTINCT标签。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。tagger_id= ?和的Tagging。tagger_type=? ORDER BY taggings_count递减LIMIT 20 [[tagger_id,1],[tagger_type,用户]
   (0.1毫秒)SELECT COUNT(*)FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。ID=? LIMIT 1 [身份证,1]
  CACHE(0.0ms)选择习惯。* FROM习惯WHERE习惯。ID=? LIMIT 1 [ID,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。user_ID的=?与习惯。ID=? LIMIT 1 [user_ID的,1],[身份证,1]
  CACHE(0.0ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  ActsAsTaggableOn ::标记加载(0.1毫秒)选择标记。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。taggable_id=?和的Tagging。taggable_type=?与(taggings.context =标签)的GROUP BY tags.id [[taggable_id,1],[taggable_type,习惯]]
  注意负载(0.1毫秒)选择注意事项。* FROM注意事项WHERE注意事项。habit_id=? ORDER BY notes_date [habit_id,1]
  渲染票据/ _notes.html.erb(1.1ms)
  渲染票据/ _form.html.erb(的3.2ms)
  用户负载(0.1毫秒)选择用户。*从用户INNER JOINhabit_likesON用户,ID=habit_likes。user_ID的WHEREhabit_likes。habit_id=? [habit_id,1]
  评论加载(0.1毫秒)选择注释。* FROM意见WHERE意见。habit_id=? [habit_id,1]
  渲染评论/ _comments.html.erb(0.9ms)
  渲染评论/ _form.html.erb(1.1ms)
  渲染习惯/布局内show.html.erb /应用程序(21.4ms)
   (0.1毫秒)SELECT COUNT(*)FROM通知WHERE通知。user_ID的=? [user_ID的,1]
  渲染布局/ _header.html.erb(2.3ms)
   (0.1毫秒)SELECT COUNT(*)FROM习惯WHERE习惯。user_ID的=?与1 = 0 [[USER_ID,1]]
  渲染习惯/ _today.html.erb(0.9ms)
  目标负荷(为0.2ms),选择目标。* FROM目标WHERE目标。user_ID的=?与目标,完成='F'ORDER BY期限LIMIT 3 [user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM用户INNER JOIN ON用户的关系,ID=关系。followed_idWHERE关系。follower_id=? [follower_id,1]
   (0.1毫秒)SELECT COUNT(*)FROM目标WHERE目标。user_ID的=?与目标,完成='F'[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROMquantifiedsWHEREquantifieds。user_ID的=? [user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM用户INNER JOIN ON用户的关系,ID=关系。follower_idWHERE关系。followed_id=? [followed_id,1]
   (0.1毫秒)SELECT COUNT(*)FROM目标WHERE目标。user_ID的=?与目标,完成=T[[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM结果INNER JOINquantifiedsON结果。quantified_id=quantifieds。IDWHEREquantifieds。user_ID的=?和结果,坏='F'[user_ID的,1]
   (0.1毫秒)SELECT COUNT(*)FROM估值WHERE估值。user_ID的=? [user_ID的,1]
  渲染布局/ _count.html.erb(9.3ms)
  渲染布局/ _tags.html.erb(0.0ms)
  渲染布局/ _recommendations.html.erb(0.1毫秒)
  量化负荷(0.1毫秒)选择quantifieds。* FROMquantifiedsWHEREquantifieds。user_ID的=?与quantifieds。类别=? [user_ID的,1],[类,平均]
  量化负荷(0.1毫秒)选择quantifieds。* FROMquantifiedsWHEREquantifieds。user_ID的=?与quantifieds。类别=? [user_ID的,1],[类,实例]
   (0.1毫秒)SELECT COUNT(*)FROM估值WHERE估值。user_ID的=?与(1 = 0)[[USER_ID,1]]
  渲染布局/ _value.html.erb(为0.8ms)
  渲染布局/ _sidebar.html.erb(17.6ms)
完成200 OK在318ms(浏览次数:301.9ms | ActiveRecord的:3.1ms)
 

显示页调用AJAX

 < D​​IV CLASS =罢工>
  <%@如果habit.current_level_strike%GT;
    < D​​IV CLASS =BTNID =红色> <标签ID =<%= @ habit.id%>中类=习惯-ID>打击:< /标签>
  <%其他%GT;
    < D​​IV CLASS =BTNID =金与GT; <标签ID =<%= @ habit.id%>中类=习惯-ID的两个>打击:< /标签>
  <%结束%GT;
    <%@ habit.levels.each_with_index办|水平,指数| %>
      <%@如果habit.current_level> =(索引+ 1)%>
        &其中p为H.;
          <%@如果habit.current_level_strike%GT;
            <标签ID =<%= level.id%>中类=级ID>级<%=指数+ 1%>:< /标签>
          <%其他%GT;
            <标签ID =<%= level.id%>中类=级-ID的两个>级<%=指数+ 1%>:< /标签>
          <%结束%GT;
          <%= check_box_tag为零,真正的,level.missed_days> 0,{类:习惯检查}%>
          <%= check_box_tag为零,真正的,level.missed_days> 1,{类:习惯检查}%>
          <%= check_box_tag为零,真正的,level.missed_days> 2,{类:习惯检查}%>
       &所述; / P>
      <%结束%GT;
    <%结束%GT;
  < / DIV>
< / DIV>
 

这是什么AJAX触发到, days_missed_controller.rb

 类DaysMissedController<的ApplicationController
  before_action:LOGGED_IN_USER,只有:[:创建:摧毁]

  DEF创建
    习惯= Habit.find(PARAMS [:habit_id])
    habit.missed_days = habit.missed_days + 1
    @ habit.save!
    水平= habit.levels.find(PARAMS [:level_id])
    level.missed_days = level.missed_days + 1
    如果level.missed_days == 3
      level.missed_days = 0
      level.days_lost + = habit.calculate_days_lost + 1
    结束
    level.save!
    头:OK#此方法返回一个200成功状态code空响应
  结束

  DEF破坏
    习惯= Habit.find(PARAMS [:habit_id])
    habit.missed_days = habit.missed_days  -  1
    habit.save!
    水平= habit.levels.find(PARAMS [:level_id])
    level.missed_days = level.missed_days  -  1
    level.save!
    头:OK#此方法返回一个200成功状态code空响应
  结束
结束
 

这里是 要点。对不起,我是新来的AJAX,并得到与此所以我的描述也许小学程度的帮助。请不要犹豫,要求进一步澄清或code: - ]


更新

因此​​,换句话说,如果用户加载显示页面,然后检查复选框没有被发送到服务器;如果用户加载显示页面,然后刷新它会检查这个帖子是在服务器的复选框:

 开始邮报/习惯/ 1 /级别/ 1 / days_missed为127.0.0.1,在2015年6月22日2点十五分21秒-0400
处理由DaysMissedController#创建为* / *
  参数:{habit_id=>中1,level_id=>中1}
  用户负载(为0.2ms),选择用户。*从用户WHERE用户,ID=? LIMIT 1 [身份证,1]
  习惯负载(为0.2ms)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  ActsAsTaggableOn ::标记加载(0.3ms)选择标记。* FROM标签WHERE(LOWER(名称)= LOWER('根深蒂固'))
  CACHE(0.0ms)选择习惯。* FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  负载级(为0.2ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  ActsAsTaggableOn ::标记加载(0.3ms)SELECT DISTINCT标签。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。tagger_id= ?和的Tagging。tagger_type=? ORDER BY taggings_count递减LIMIT 20 [[tagger_id,1],[tagger_type,用户]
   (0.1毫秒)SELECT COUNT(*)FROM习惯WHERE习惯。user_ID的=? [user_ID的,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。user_ID的=?与习惯。ID=? LIMIT 1 [user_ID的,1],[身份证,1]
  习惯负载(0.1毫秒)选择习惯。* FROM习惯WHERE习惯。ID=? LIMIT 1 [身份证,1]
   (0.1毫秒)开始交易
  CACHE(0.0ms)选择水平。* FROM水平WHERE水平。habit_id=? [habit_id,1]
  CACHE(0.0ms)选择用户。*从用户WHERE用户,ID=? LIMIT 1 [身份证,1]
  ActsAsTaggableOn ::标记加载(为0.2ms),选择标签。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。taggable_id=?和的Tagging。taggable_type=?与(taggings.context =标签与taggings.tagger_id IS NULL)[[taggable_id,1],[taggable_type,习惯]]
  CACHE(0.0ms)选择标记。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。taggable_id=?和的Tagging。taggable_type=?与(taggings.context =标签与taggings.tagger_id IS NULL)[[taggable_id,1],[taggable_type,习惯]]
  ActsAsTaggableOn ::标记加载(0.1毫秒)选择标记。* FROM标签INNER JOIN的TaggingON标签。ID=的Tagging。tag_idWHERE的Tagging。taggable_id=?和的Tagging。taggable_type=? AND(taggings.context =标签和
                                    taggings.tagger_id = 1 AND
                                    taggings.tagger_type ='用户')[[taggable_id,1],[taggable_type,习惯]]
   (0.1毫秒)提交事务
  负载级(为0.2ms)选择水平。* FROM水平WHERE水平。habit_id=?和水平,ID=? LIMIT 1 [habit_id,1],[身份证,1]
   (0.1毫秒)开始交易
  SQL(0.3ms)UPDATE水平SETmissed_days=?的updated_at=? WHERE水平,ID=? [[missed_days,1],[的updated_at,2015年6月22日06:15:21.959609],[身份证,1]]
   (2.8ms)提交事务
完成200 OK在40毫秒(ActiveRecord的:5.4ms)
 

解决方案

这可能与Turbolinks问题,可以尝试改变你的javascript:

  $(文件)。就绪(函数()
{
  // ...
}
 

看起来像:

  $(文件)。在(准备页:加载功能(){
  // ..
}
 

请让我知道,如果它帮助!

祝你好运!

Sometimes this AJAX fires & sometimes it doesn't, let me explain.

habit.js

$(document).ready(function()
{
  $(".habit-check").change(function()
  {
    habit = $(this).parent().siblings(".habit-id").first().attr("id");
    level = $(this).siblings(".level-id").first().attr("id");
    if($(this).is(":checked"))
    {
       $.ajax(
       {
         url: "/habits/" + habit + "/levels/" + level + "/days_missed",
         method: "POST"
       });
    }
    else
    {
       $.ajax(
       {
         url: "/habits/" + habit + "/levels/" + level + "/days_missed/1",
         method: "DELETE"
       });
    }
  });
});

It only fires after I load the show page AND refresh it so that the terminal looks like this:

Started GET "/habits/1" for ::1 at 2015-06-20 23:55:42 -0400
Processing by HabitsController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.1ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", "1"]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags') GROUP BY tags.id  [["taggable_id", 1], ["taggable_type", "Habit"]]
  Note Load (0.1ms)  SELECT "notes".* FROM "notes" WHERE "notes"."habit_id" = ?  ORDER BY notes_date  [["habit_id", 1]]
  Rendered notes/_notes.html.erb (2.0ms)
  Rendered notes/_form.html.erb (3.9ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "habit_likes" ON "users"."id" = "habit_likes"."user_id" WHERE "habit_likes"."habit_id" = ?  [["habit_id", 1]]
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."habit_id" = ?  [["habit_id", 1]]
  Rendered comments/_comments.html.erb (0.8ms)
  Rendered comments/_form.html.erb (1.0ms)
  Rendered habits/show.html.erb within layouts/application (24.0ms)
   (0.2ms)  SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_header.html.erb (2.9ms)
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? AND 1=0  [["user_id", 1]]
  Rendered habits/_today.html.erb (1.0ms)
  Goal Load (0.2ms)  SELECT  "goals".* FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  ORDER BY deadline LIMIT 3  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "quantifieds" WHERE "quantifieds"."user_id" = ?  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 't'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "results" INNER JOIN "quantifieds" ON "results"."quantified_id" = "quantifieds"."id" WHERE "quantifieds"."user_id" = ? AND "results"."bad" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_count.html.erb (10.7ms)
  Rendered layouts/_tags.html.erb (0.1ms)
  Rendered layouts/_recommendations.html.erb (0.0ms)
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Averaged"]]
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Instance"]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ? AND (1 = 0)  [["user_id", 1]]
  Rendered layouts/_value.html.erb (0.8ms)
  Rendered layouts/_sidebar.html.erb (18.9ms)
Completed 200 OK in 324ms (Views: 307.4ms | ActiveRecord: 3.3ms)


Started GET "/assets/application-2b528cffd402884831208f505b651927.css?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/jquery-ea2706bc2d0586731b7c00ec2cd0a414.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/stylesheet-3e834b3a950daa6165191fa42b13a968.css?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/jquery_ujs-29277b946c1d3754af13672bfec0c660.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/bootstrap-d16805677947f8c7ce91cea7ae7f82b0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/cocoon-07a2b3765fb91baf2df1cea98d97eed0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/turbolinks-3a7cdb8630f3e86770867bd488a098e0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/account_activations-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/comments-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/days_missed-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/facebook-faaf7b74474e5a746a0c4a160e4705d2.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/habit-ff66f57b87d00b4469848f1af3f6910a.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/habits-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/activities-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/pages-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/password_resets-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/notes-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/quantified-5cae96afee8ab50e506c00828f3042b3.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/goals-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/quantifieds-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/relationships-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/sessions-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/notifications-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/tags-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/users-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/values-8f6eecd1178ef85964369ec66d33c4f1.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/application-6fd7d78d93af9f6901c08c3bc66303d0.js?body=1" for ::1 at 2015-06-20 23:55:42 -0400


Started GET "/assets/bootstrap/glyphicons-halflings-regular-195cb410b49d75c104a5bc6ad385ac77.woff" for ::1 at 2015-06-20 23:55:43 -0400

Just loading the show page once won't do the job because the checkmark doesn't fire the AJAX (Notice all the GET above & lack of GET below, I think it has something to do with turbolinks).

Started GET "/habits/1" for ::1 at 2015-06-21 00:09:02 -0400
Processing by HabitsController#show as HTML
  Parameters: {"id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.1ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", "1"]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags') GROUP BY tags.id  [["taggable_id", 1], ["taggable_type", "Habit"]]
  Note Load (0.1ms)  SELECT "notes".* FROM "notes" WHERE "notes"."habit_id" = ?  ORDER BY notes_date  [["habit_id", 1]]
  Rendered notes/_notes.html.erb (1.1ms)
  Rendered notes/_form.html.erb (3.2ms)
  User Load (0.1ms)  SELECT "users".* FROM "users" INNER JOIN "habit_likes" ON "users"."id" = "habit_likes"."user_id" WHERE "habit_likes"."habit_id" = ?  [["habit_id", 1]]
  Comment Load (0.1ms)  SELECT "comments".* FROM "comments" WHERE "comments"."habit_id" = ?  [["habit_id", 1]]
  Rendered comments/_comments.html.erb (0.9ms)
  Rendered comments/_form.html.erb (1.1ms)
  Rendered habits/show.html.erb within layouts/application (21.4ms)
   (0.1ms)  SELECT COUNT(*) FROM "notifications" WHERE "notifications"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_header.html.erb (2.3ms)
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ? AND 1=0  [["user_id", 1]]
  Rendered habits/_today.html.erb (0.9ms)
  Goal Load (0.2ms)  SELECT  "goals".* FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  ORDER BY deadline LIMIT 3  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."followed_id" WHERE "relationships"."follower_id" = ?  [["follower_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "quantifieds" WHERE "quantifieds"."user_id" = ?  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "users" INNER JOIN "relationships" ON "users"."id" = "relationships"."follower_id" WHERE "relationships"."followed_id" = ?  [["followed_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "goals" WHERE "goals"."user_id" = ? AND "goals"."accomplished" = 't'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "results" INNER JOIN "quantifieds" ON "results"."quantified_id" = "quantifieds"."id" WHERE "quantifieds"."user_id" = ? AND "results"."bad" = 'f'  [["user_id", 1]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ?  [["user_id", 1]]
  Rendered layouts/_count.html.erb (9.3ms)
  Rendered layouts/_tags.html.erb (0.0ms)
  Rendered layouts/_recommendations.html.erb (0.1ms)
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Averaged"]]
  Quantified Load (0.1ms)  SELECT "quantifieds".* FROM "quantifieds" WHERE "quantifieds"."user_id" = ? AND "quantifieds"."categories" = ?  [["user_id", 1], ["categories", "Instance"]]
   (0.1ms)  SELECT COUNT(*) FROM "valuations" WHERE "valuations"."user_id" = ? AND (1 = 0)  [["user_id", 1]]
  Rendered layouts/_value.html.erb (0.8ms)
  Rendered layouts/_sidebar.html.erb (17.6ms)
Completed 200 OK in 318ms (Views: 301.9ms | ActiveRecord: 3.1ms)

This show page calls the AJAX

<div class="strikes">
  <% if @habit.current_level_strike %> 
    <div class="btn" id="red"> <label id="<%= @habit.id %>" class="habit-id">Strikes:</label>
  <% else %> 
    <div class="btn" id="gold"> <label id="<%= @habit.id %>" class="habit-id-two">Strikes:</label>
  <% end %>
    <% @habit.levels.each_with_index do |level, index| %>
      <% if @habit.current_level >= (index + 1) %>
        <p>
          <% if @habit.current_level_strike %> 
            <label id="<%= level.id %>" class="level-id">Level <%= index + 1 %>:</label> 
          <% else %> 
            <label id="<%= level.id %>" class="level-id-two">Level <%= index + 1 %>:</label> 
          <% end %>
          <%= check_box_tag nil, true, level.missed_days > 0, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 1, {class: "habit-check"} %>
          <%= check_box_tag nil, true, level.missed_days > 2, {class: "habit-check"} %>
       </p>
      <% end %>
    <% end %>
  </div>
</div>

This is what the AJAX fires to, days_missed_controller.rb.

class DaysMissedController < ApplicationController
  before_action :logged_in_user, only: [:create, :destroy]

  def create
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days + 1
    @habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days + 1
    if level.missed_days == 3
      level.missed_days = 0
      level.days_lost += habit.calculate_days_lost + 1
    end
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end

  def destroy
    habit = Habit.find(params[:habit_id])
    habit.missed_days = habit.missed_days - 1
    habit.save!
    level = habit.levels.find(params[:level_id])
    level.missed_days = level.missed_days - 1
    level.save!
    head :ok # this returns an empty response with a 200 success status code
  end
end

Here's the gist of it. Sorry I'm new to AJAX and got help with this so my level of description maybe elementary. Please don't hesitate to ask for further clarification or code :-]


UPDATE

So in other words if a user loads the show page and then checks the checkboxes nothing is sent to the server; If the user loads the show page AND then refreshes it then checks the checkboxes this POST is made in the server:

Started POST "/habits/1/levels/1/days_missed" for 127.0.0.1 at 2015-06-22 02:15:21 -0400
Processing by DaysMissedController#create as */*
  Parameters: {"habit_id"=>"1", "level_id"=>"1"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Habit Load (0.2ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT "tags".* FROM "tags" WHERE (LOWER(name) = LOWER('ingrain'))
  CACHE (0.0ms)  SELECT "habits".* FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Level Load (0.2ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  ActsAsTaggableOn::Tag Load (0.3ms)  SELECT  DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."tagger_id" = ? AND "taggings"."tagger_type" = ?  ORDER BY taggings_count desc LIMIT 20  [["tagger_id", 1], ["tagger_type", "User"]]
   (0.1ms)  SELECT COUNT(*) FROM "habits" WHERE "habits"."user_id" = ?  [["user_id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."user_id" = ? AND "habits"."id" = ? LIMIT 1  [["user_id", 1], ["id", 1]]
  Habit Load (0.1ms)  SELECT  "habits".* FROM "habits" WHERE "habits"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  CACHE (0.0ms)  SELECT "levels".* FROM "levels" WHERE "levels"."habit_id" = ?  [["habit_id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  ActsAsTaggableOn::Tag Load (0.2ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Habit"]]
  CACHE (0.0ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL)  [["taggable_id", 1], ["taggable_type", "Habit"]]
  ActsAsTaggableOn::Tag Load (0.1ms)  SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = ? AND "taggings"."taggable_type" = ? AND (taggings.context = 'tags' AND
                                    taggings.tagger_id = 1 AND
                                    taggings.tagger_type = 'User')  [["taggable_id", 1], ["taggable_type", "Habit"]]
   (0.1ms)  commit transaction
  Level Load (0.2ms)  SELECT  "levels".* FROM "levels" WHERE "levels"."habit_id" = ? AND "levels"."id" = ? LIMIT 1  [["habit_id", 1], ["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.3ms)  UPDATE "levels" SET "missed_days" = ?, "updated_at" = ? WHERE "levels"."id" = ?  [["missed_days", 1], ["updated_at", "2015-06-22 06:15:21.959609"], ["id", 1]]
   (2.8ms)  commit transaction
Completed 200 OK in 40ms (ActiveRecord: 5.4ms)

解决方案

This might be the problem with Turbolinks, could you try changing your javascript:

$(document).ready(function()
{
  // ...
}

to look like:

$(document).on("ready page:load", function() {
  // ..
}

Please, let me know if it helped!

Good luck!

这篇关于如何解决AJAX检查框时,应时刻火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆