【问题标题】:How to show only scoped instances of Task model in view如何在视图中仅显示任务模型的范围实例
【发布时间】:2021-08-07 06:31:24
【问题描述】:

我正在 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', 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

这是控制器

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 }
        format.json { render json: @task.errors, 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

【问题讨论】:

    标签: ruby-on-rails ruby model-view-controller ruby-on-rails-5


    【解决方案1】:

    我假设你有一个 TasksController 和一个 index 方法。

    在索引方法中尝试以下

    def index
      @tasks = Task.due_today
    end
    

    让我知道这是否有效

    【讨论】:

    • 你能用控制器代码更新问题吗?
    • 已更新。请记住,我想在 index.html.erb 中显示所有任务,并且只有今天到期的任务
    【解决方案2】:

    您需要更改 due_date 范围。使用 between 或 date_trunc 方法。

    where("date_trunc('day', duedate)::date = ?", Date.today)

    让我知道它是否有效

    【讨论】:

      【解决方案3】:

      让它工作。控制器没有变化。不需要。这是代码。

      这是任务模型。

      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>
      

      【讨论】:

        猜你喜欢
        • 2011-10-21
        • 1970-01-01
        • 2013-03-17
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多