0

我的促销有保质期(到期日),到期后如何销毁??我想控制日期,Date.today.to_s因为以这种形式返回"YYYY-MM-DD"我的数据库中的日期。

但是我有控制器显示并销毁我把它放在哪里?

   def show
@promotion = Promotion.find(params[:id])

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @promotion }
end
end

     def destroy
@promotion = Promotion.find(params[:id])
if @promotion.user == current_user #if user is the owner of that promotion
  @promotion.destroy
  respond_to do |format|
    format.html { redirect_to promotions_url, notice:'Promotion was successfully delete.' }
    format.json { head :ok }
  end
else
  redirect_to root_path ,:alert => 'This is not your promotion, you can delete only those who have entered.'
    end
  end
end
4

1 回答 1

0

你应该玩 cron 工作

我有应用程序并clockwork gem用于处理scheduled application tasks

你可以在这里使用发条宝石

例子


首先放入您的gemfile

gem 'clockwork'
gem 'foreman'

bundle install

二次创作yourapp/app/clock.rb

require './config/boot'
require './config/environment'
require 'clockwork'

module Clockwork
 # every one day at midnight cron schedule 

 every(1.day, 'promo.expired', :at => '00:00') {

   # find all promotion with codition :expiration_date equal to date today
   # if :expiration_date with type Date

   @promotions = Promotion.all(:conditions => { :expiration_date = DateTime.now.to_date })   
   @promotions.each do |promo|
     # destroy promo
     promo.destroy
   end
 }
end

三、我们测试一下

clockworkd -c clock.rb start

如果成功,您可以使用foreman start运行计划的每个启动应用程序

这是工头的截屏视频

希望这有帮助。

于 2013-05-29T18:41:48.190 回答