如何解决如何在视图中仅显示任务模型的范围实例
我正在用 Rails 构建一个任务管理器。我使用范围来获取今天到期的任务模型的所有实例。在 Rails 控制台中对 Tasks 调用 Due_today 时,Task 模型上的作用域有效并且仅返回今天到期的 Tasks。但是我似乎无法在视图中显示今天到期的这些任务。我需要添加条件吗?
这是我的观点 index.html.erb
<p id="notice"><%= notice %></p>
<h1>Team's Tasks</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show',task %></td>
<td><%= link_to 'Edit',edit_task_path(task) %></td>
<td><%= link_to 'Destroy',task,method: :delete,data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<h1>Tasks due today.</h1>
<table>
<thead>
<tr>
<th>Task title</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show',data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task',new_task_path %>
</p>
这是我的任务模型
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue,-> { where("duedate < ?",Time.Now) }
#use scope on Task model to get only tasks that are due today.
scope :due_today,->{ where("duedate >= ? AND duedate <= ?",Date.current.beginning_of_day,Date.current.end_of_day) }
end
这是控制器
class TasksController < ApplicationController
before_action :set_task,only: %i[ show edit update destroy ]
# GET /tasks or /tasks.json
# Order all tasks by ascending order
def index
@tasks = Task.all.order(duedate: :asc)
end
# GET /tasks/1 or /tasks/1.json
def show
end
# GET /tasks/new
def new
@task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks or /tasks.json
def create
@task = Task.new(task_params)
respond_to do |format|
if @task.save
format.html { redirect_to @task,notice: "Task was successfully created." }
format.json { render :show,status: :created,location: @task }
else
format.html { render :new,status: :unprocessable_entity }
format.json { render json: @task.errors,status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1 or /tasks/1.json
def update
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to @task,notice: "Task was successfully updated." }
format.json { render :show,status: :ok,location: @task }
else
format.html { render :edit,status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1 or /tasks/1.json
def destroy
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url,notice: "Task was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end
# Only allow a list of trusted parameters through.
def task_params
params.require(:task).permit(:title,:description,:duedate,:completed)
end
end
解决方法
我假设你有一个 TasksController 和一个 index 方法。
尝试使用索引方法
let attempt = 1;
let rN;
function EmitRandomNumber() {
return new Promise((resolve,reject)=> {
console.log(`Attempt #${attempt}. EmitRandomNumber is called.`);
setTimeout(()=>{
let randomNumber = Math.floor(Math.random() * 100) + 1;
rN = randomNumber;
console.log("2 seconds have passed.");
if(randomNumber>=80&&attempt<=10){
console.log(randomNumber,attempt);
resolve();
}else if(randomNumber<80&&attempt<=10){
attempt++;
console.log(`Random number generated is ${randomNumber}.`);
console.log("===============================");
EmitRandomNumber();
}
},2000);
});
}
let promise = EmitRandomNumber();
promise.then(()=>{
console.log(`Random number generated is ${rN}!!!!`);
console.log("===============================");
}).catch(()=>{
console.log("End");
});
告诉我这是否有效
,您需要更改 due_date 范围.. 使用 between 或 date_trunc 方法。
where("date_trunc('day',Duedate)::date = ?",Date.today)
告诉我它是否有效
,开始工作。控制器没有变化。不需要。这是代码。
这是任务模型。
class Task < ApplicationRecord
#use scope on Task model to mark overdue Tasks if the present date is greater than duedate.
scope :overdue,-> { where("duedate < ?",Time.now) }
#use scope on Task model to get Tasks due today.
scope :due_today,->{ where("duedate >= ? AND duedate <= ?",Date.current.beginning_of_day,Date.current.end_of_day) }
def overdue?
duedate < Time.now
end
def due_today?
duedate >= Date.current.beginning_of_day && duedate <= Date.current.end_of_day
end
end
这是任务视图。
<p id="notice"><%= notice %></p>
<h1>Team Task Manager</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show',task %></td>
<td><%= link_to 'Edit',edit_task_path(task) %></td>
<td><%= link_to 'Destroy',task,method: :delete,data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<br>
<h1>DUE TODAY!</h1>
<table>
<thead>
<tr>
<th>Task</th>
<th>Description</th>
<th>Date assigned</th>
<th>Due date</th>
<th>Overdue?</th>
<th>Completed?</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @tasks.due_today.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.description %></td>
<td><%= task.created_at %></td>
<td><%= task.duedate %></td>
<td><%= task.overdue? %></td>
<td><%= task.completed %></td>
<td><%= link_to 'Show',data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<p>
<%= link_to 'New Task',new_task_path %>
</p>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。