在我的Rails 3项目中,我有
的以下代码我的控制器:
class TasksController < ApplicationController
def today
@tasks = Task.today
@task = Task.new
respond_to do |format|
format.html { render :text=> "Sorry , you don't have any task pending today." }
format.html # new.html.erb
format.xml { render :xml => @tasks }
end
end
def this_week
@tasks = Task.this_week
@task = Task.new
respond_to do |format|
format.html { render :text => "Sorry , No content for selected period." }
format.html # new.html.erb
format.xml { render :xml => @tasks }
end
end
end
我的模特:
class Task < ActiveRecord::Base
def self.today
Task.where(:due_date => "Date.today" , :task_status => "open").order("due_date ASC")
end
def self.this_week
Task.where(:due_date =>"Time.now.this_week" , :task_status => "open" ).order("due_date ASC")
end
end
为什么它不在相对视图中显示任何内容。请帮帮我。
由于
答案 0 :(得分:0)
执行Task.where(...)
时,您将字符串传递给:due_date
,而不是日期。删除引号:
Task.where(:due_date => Date.today, :task_status => "open").order("due_date ASC")
对于你的第二个,没有名为Time.now.this_week
的方法。您需要提供between
范围:
Task.where("due_date between ? and ?", Date.today.beginning_of_week, Date.today.end_of_week").where(:task_status => "open").order("due_date ASC")