task_controller.rb

来自「基于ruby的进阶编程,大家可以对照教材<<Ruby on Rail」· RB 代码 · 共 62 行

RB
62
字号
class TaskController < ApplicationController
	before_filter :set_charset 
	
	def set_charset 
		@headers["Content-Type"] = "text/html; charset=GB2312" 
	end 
	
	def index
		@editable = true
		@current_task = Task.find(:first, :conditions => ['isdone =?', 1])
		@editable = false if @current_task
		@success = Task.count(['isdone =?', 2])
		@failures = Task.count(['isdone =?', 3])
		@history = []

		ary = Task.find(:all, :conditions => ['not isdone =?', 1]).reverse
		if ary.size >= 8
			ary[0..7].each {|ar|
				x = TaskHelper::time_convert(Time.now.to_i - ar.finished)
				@history << {'state' => ar.state.value, 'body' => ar.body, 'time' => x }
			}
		else
			ary.each {|ar|
				x = TaskHelper::time_convert(Time.now.to_i - ar.finished)
				@history << {'state' => ar.state.value, 'body' => ar.body, 'time' => x }
			}
			(8 - ary.size).times { @history << {'state' => ' ', 'body' => ' ', 'time' => ' ' } }
		end
	end

	def create
		task = Task.new(params[:task])
		task.uid = 1
		task.created_at = params[:task][:created_at]
		task.body = params[:task][:created_at] + ' ' + params[:task][:body]
		task.finished = 0
		task.save

		redirect_to :action => 'index'
	end

	def eval
		task = Task.find(params[:task][:id])
		task.finished = Time.now.to_i
		params.each {|key, value|
			case key
			when 'action=done'
				task.isdone = 2
			when 'action=failed'
				task.isdone = 3
			end
		}
		task.save

		redirect_to :action => 'index'
	end

	def clear
		Task.destroy_all(['uid =?', 1])
		redirect_to :action => 'index'
	end
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?