我想在待辦事項列表應用程序中做任務,待辦事項將在7天后自毀。然而,就我現在的代碼而言,日期從創建待辦事項日期開始計算,而不是倒計時然後被銷燬。任何想法如何解決這個將不勝感激。耙子任務不能正常工作的自毀功能
這裏是我的待辦事項部分中的觀點:
<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 %>
如果您需要查看其他文件,請讓我知道。再次感謝!
編輯:添加的文件:
這裏是我的schedule.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, '[email protected]:me/my_repo.git'
我需要把填寫set:application/set:repo_url以使deploy.rb文件正常工作?謝謝!
你可以使用'7.days.ago'而不是'Time.now - 7.days'來使耙子任務更加可讀。 – pjmorse 2014-09-25 17:55:05
耙子任務是否工作?也就是說,如果你運行'rake delete_items',它是否刪除了項目?這應該。是運行rake任務的代碼?我在這裏看到的只是模板和Javascript。 – pjmorse 2014-09-25 18:01:23
最後,'distance_of_time_in_words_to_now(todo.created_at + 7.days)' - 你期望怎麼說?如果我剛創建了待辦事項,應該說「7天」。如果我明天回來,應該說「6天」。對? – pjmorse 2014-09-25 18:06:38