耙任务不起作用的自毁功能
Self-destructing feature with rake task not working
我正在尝试在我的待办事项列表应用程序中创建一个功能,在该功能中,待办事项将在7天后自毁。然而,在我现在的代码中,从创建todo的日期开始,天数是递增的,而不是递减然后被销毁。任何关于如何解决这个问题的想法都将不胜感激。
以下是我要做的部分视图:
<tr id="<%= dom_id(todo)%>">
<td><%= todo.description %></td>
<td><%= distance_of_time_in_words_to_now(todo.created_at + 7.days) %></td>
<td><%= link_to "Completed", todo, method: :delete, remote: true, class: "btn btn-success" %></td>
</tr>
这是我的rake任务代码:
desc 'Removes items over a week old'
task delete_items: :environment do
Todo.where("created_at <= ?", Time.now - 7.days).destroy_all
end
这是我的destroy.js.erb文件:
<% if @todo.destroyed? %>
$('#todo_' +<%= @todo.id %>).hide();
$('.alert').html("<button type='button' class='close' data-dismiss='alert'>×</button><%= flash[:notice] %>");
$('.alert').addClass('alert-success');
$('.alert').removeClass('remove');
<% else %>
$('.alert').html("<button type='button' class='close' data-dismiss='alert'>×</button><%= flash[:error] %>");
$('.alert').addClass('alert-danger');
$('.alert').removeClass('remove');
<% end %>
这是我的create.js.erb文件:
<% if @todo.valid? %>
$('.js-todos').prepend("<%= escape_javascript(render(@todo)) %>");
$('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>");
$('.alert').html("<button type='button' class='close' data-dismiss='alert'>×</button><%= flash[:notice] %>");
$('.alert').addClass('alert-success');
$('.alert').removeClass('remove');
<% else %>
$('.alert').html("<button type='button' class='close' data-dismiss='alert'>×</button><%= flash[:error] %>");
$('.alert').addClass('alert-danger');
$('.alert').removeClass('remove');
$('.new-todo').html("<%= escape_javascript(render partial: 'todos/form') %>")
<% end %>
如果您需要查看其他文件,请告诉我。再次感谢!
编辑:添加的文件:
这是我的日程安排。rb代码:
set :output, "#{path}/log/cron.log"
every 1.day do
rake "delete_items"
end
这是我的新deploy.rb文件:
require "bundler/capistrano"
set :whenever_command, "bundle exec whenever"
require "whenever/capistrano"
# config valid only for Capistrano 3.1
lock '3.2.1'
set :application, 'my_app_name'
set :repo_url, 'git@example.com:me/my_repo.git'
我是否需要在set:application/set:repo_url中填充deploy.rb文件才能工作?谢谢
您的表将始终递增。忘记distance_of_time_in_words
方法的实现复杂性,只需考虑它的含义:如果你今天创建一个项,那么它明天的时间距离将是1天。然后后天两天。然后3天。将7天添加到创建的时间中只会使其为-6天,然后为-5天,依此类推(忽略d_o_t_i_w_f_n
如何格式化它)。当你点击0时,todo应该被删除,并且不会显示在表上,但表只是显示一些东西——它与项目是否被删除没有实际关系;它只是通知用户。删除由您编写的rake任务负责,当运行时,该任务似乎正在运行。
听起来问题是你的任务根本没有运行。根据您的报告,当您手动运行它时,它是工作的,但您的调度程序无法定期运行它。你安排得怎么样?调度程序是否不同时在本地和生产环境中工作?你用什么工具来安排日程,你是如何使用它们的?
因为你的表和调度程序是单独的问题,我建议你问一个关于rake调度失败的新问题,在这个问题中,你发布了你的调度代码(如果你使用了什么宝石?你是如何使用它们的?在本地和生产上显式运行任务(在调度程序之外)的行为是什么?),并且尝试确定调度rake任务的哪个部分失败。
你的表格看起来基本正确。如果它无法显示所需的单词/数字,那只是distance_of_time_in_words
方法的一个函数,以及你如何使用它
include ActionView::Helpers::DateHelper # Module of which d_o_t_i_w_f_n is a method
distance_of_time_in_words_from_now(5.days.ago)
distance_of_time_in_words_from_now(24.hours.from_now)
事实上,您想要删除的项目显示在表中,这可能与日程安排有关,而与表逻辑无关,所以在这个问题上,我只关注日程安排。
- 添加文字和评论功能更新Div
- JavaScript打印功能使日历停止工作
- 每当您在选择器内移动鼠标时,悬停功能就会重复
- 如何防止网页加载后自动启动功能
- 除修剪外的其他功能
- 悬停功能触发器
- 使用angularjs向浏览器发送servlet响应(下载功能)
- 删除CKEditor工具栏按钮,但不删除功能
- 当使用Watchify时,Browserify+Babelify Gulp任务没有终止
- 异步facebook功能
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- jQuery滚动功能只工作一次
- Graphiti中是否有任何工具提示功能
- gulp Iconify任务抛出错误TypeError:Path必须是字符串.收到false
- React redux初始化功能,无论状态变化如何
- 在哪里可以学习ECMAScript标准中尚未包含的JavaScript功能
- 正在获取select上的功能id
- 耙任务不起作用的自毁功能
- 在实习生中进行功能测试时,使用辅助模块执行重复的、有序的任务
- 确保在前一个功能完成后启动下一个功能.在任务完成之前,禁用所有目标元素的已注册事件