Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在视图中仅显示任务模型的作用域实例_Ruby On Rails_Ruby_Model View Controller_Ruby On Rails 5 - Fatal编程技术网

Ruby on rails 如何在视图中仅显示任务模型的作用域实例

Ruby on rails 如何在视图中仅显示任务模型的作用域实例,ruby-on-rails,ruby,model-view-controller,ruby-on-rails-5,Ruby On Rails,Ruby,Model View Controller,Ruby On Rails 5,我正在Rails中构建一个任务管理器。我使用scope获取今天到期的任务模型的所有实例。在Rails控制台中对任务调用due_today时,任务模型上的作用域工作,并且只返回今天到期的任务。但我似乎无法在视图中显示这些今天到期的任务。我需要添加一个条件吗 这是我的视图index.html.erb <p id="notice"><%= notice %></p> <h1>Team's Tasks</h1> <

我正在Rails中构建一个任务管理器。我使用scope获取今天到期的任务模型的所有实例。在Rails控制台中对任务调用due_today时,任务模型上的作用域工作,并且只返回今天到期的任务。但我似乎无法在视图中显示这些今天到期的任务。我需要添加一个条件吗

这是我的视图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', 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>

<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
类任务{where(“duedate<?”,Time.now)}
#使用任务模型上的作用域仅获取今天到期的任务。

范围:due_today,->{where(“duedate>=”和duedate我假设您在那里有一个TasksController和一个索引方法

尝试以下索引方法

def index
  @tasks = Task.due_today
end

让我知道这是否有效。您需要更改到期日范围。请使用日期间或日期间方法

其中(“日期('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

类任务{where(“duedate<?”,Time.now)}
#使用任务模型上的作用域来获取今天到期的任务。

范围:今天到期,->{where(“duedate>=”和duedate它不起作用。你能用控制器代码更新问题吗?更新。请记住,我想在index.html.erb中显示所有任务,并且只有今天到期的任务显示所有任务都能完美工作。顺便说一句,我成功了。我将发布我的代码。
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', 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>

<p>
  <%= link_to 'New Task', new_task_path %>
</p>