簡體   English   中英

如何創建復選框,設置復選框值,將它們求和為一個任務模型值並根據該值對任務進行排序?

[英]How can I create checkboxes, set checkbox values, sum them up to one task model value and sort tasks according to that value?

我正在用Rails列出待辦事項。 我希望每個任務都有兩個復選框(“重要”和“緊急”)。 我想根據兩個標准對這些任務進行排序。 第一個條件是復選框,第二個條件應是創建或更新任務的時間。 因此,按照第一個標准,應該對任務進行排序的優先級為4:

  1. 任務(已選中重要和緊急的復選框)
  2. 任務(已選中重要復選框,未選中緊急復選框)
  3. 任務(選中緊急復選框,未選中重要復選框)
  4. 任務(未選中復選框)

如果存在多個具有相同優先級的任務,則應按第二個標准降序但在第一個標准內對它們進行排序。

我將創建一個遷移,以將整數添加到任務模型,然后將復選框設置為以下值:“重要= 2”和“緊急= 1”(未經檢查的值= 0)。 這些復選框的值應加起來並鏈接到任務模型中的整數,然后在任務視圖中對它們進行排序。

問題:

  • 如何制作復選框,設置復選框值並將它們匯總為一個任務模型值?
  • 如何根據匯總值對任務進行排序?
  • 如何按創建/更新的時間作為第二標准對任務進行排序?

index.html.erb(任務視圖)

<h1>To Do</h1>

<table>
    <thead>
        <th>Content</th>
        <th>State</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </thead>

    <tbody>
        <% @to_do.each do |task| %>
        <tr>
            <td><%= task.content %></td>
            <td><%= task.state %></td>
            <td><%= link_to 'Show', task %></td>
            <td><%= link_to 'Edit', edit_task_path(task) %></td>
            <td><%= link_to 'Destroy', task_path(task), method: :delete, data: { confirm: "Are you sure to delete this task?" } %></td>
            <td><%= link_to 'Mark as Doing', change_task_path(task, state: "doing"), method: :put %></td>
        </tr>
        <% end %>
    </tbody>
</table>

<br>

<h1>Doing</h1>

<table>
    <thead>
        <th>Content</th>
        <th>State</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </thead>

    <tbody>
        <% @doing.each do |task| %>
        <tr>
            <td><%= task.content %></td>
            <td><%= task.state %></td>
            <td><%= link_to 'Show', task %></td>
            <td><%= link_to 'Edit', edit_task_path(task) %></td>
            <td><%= link_to 'Destroy', task_path(task), method: :delete, data: { confirm: "Are you sure to delete this task?" } %></td>
            <td><%= link_to 'Mark as To Do', change_task_path(task, state: "to_do"), method: :put %></td>
            <td><%= link_to 'Mark as Done', change_task_path(task, state: "done"), method: :put %></td>
        </tr>
        <% end %>
    </tbody>
</table>

<br>

<h1>Done</h1>

<table>
    <thead>
        <th>Content</th>
        <th>State</th>
        <th></th>
        <th></th>
        <th></th>
        <th></th>
    </thead>

    <tbody>
        <% @done.each do |task| %>
        <tr>
            <td><%= task.content %></td>
            <td><%= task.state %></td>
            <td><%= link_to 'Show', task %></td>
            <td><%= link_to 'Edit', edit_task_path(task) %></td>
            <td><%= link_to 'Destroy', task_path(task), method: :delete, data: { confirm: "Are you sure to delete this task?" } %></td>
            <td><%= link_to 'Mark as Doing', change_task_path(task, state: "doing"), method: :put %></td>
        </tr>
        <% end %>
    </tbody>
</table>

<br>

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

new.html.erb(任務新視圖)

<h1>New Task</h1>


 <%= form_for(@task) do |f| %>

    <%= f.text_field :content, placeholder: "Content", class: "formfield" %>

    <%= f.submit "Save Content", class: "form_button" %>
  <% end %>


<%= link_to 'Back', tasks_path %>

task_controller.rb

class TasksController < ApplicationController
  before_action :logged_in_user
  before_action :set_task, only: [:show, :edit, :update, :destroy, :change]

  def index
    @to_do = current_user.tasks.where(state: "to_do")
    @doing = current_user.tasks.where(state: "doing")
    @done = current_user.tasks.where(state: "done")
  end

  def show
  end

  def new
    @task = Task.new
  end

  def edit
  end

  def create
    @task = current_user.tasks.new(task_params)
    if @task.save
      flash[:success] = "You successfully created a Task!"
      redirect_to tasks_path
    else
      render 'new_task_path'
    end
  end

  def update
    @task.update(task_params)
    if @task.save
      flash[:success] = "You successfully updated a Task!"
      redirect_to tasks_path
    else
      render 'edit_task_path'
    end
  end

  def destroy
    @task.destroy
    flash[:success] = "You successfully deleted a Task!"
    redirect_to tasks_path
  end

  def change
    @task.update_attributes(state: params[:state])
    flash[:success] = "You successfully changed the State!"
    redirect_to tasks_path
  end



  private
    def set_task
      @task = Task.find(params[:id])
    end

    def task_params
      params.require(:task).permit(:content, :state)
    end

end

問題1:如何制作復選框,設置復選框值並將它們匯總為一個任務模型值?

我不認為將復選框值加到一個“任務模型值”是最好的解決方案。 我會在“任務”模型中同時添加“緊急”和“重要”列。 從長遠來看,這具有更大的可伸縮性,並且通常更易於使用。 然后,這只是使用Rails check_box_tag幫助器的一種情況。 http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag

問題2:如何根據匯總值對任務進行排序?

如果對於重要和緊急事件有單獨的列/屬性,則可以在任務模型上創建范圍以根據需要進行排序。

scope :important, -> { where(important: true) }
scope :urgent, -> { where(urgent: true) }

然后,您可以根據需要鏈接命名范圍。

Task.important.urgent

或者,您也可以指定最近的范圍:

scope :recent, -> { order("posts.updated_at DESC") }

然后致電:

Task.important.recent

它將運行以下SQL查詢:

#   SELECT "tasks".* FROM "tasks" WHERE "tasks"."important" = 'true' 
#   ORDER BY tasks.updated_at DESC

Q3:如何按創建/更新的時間作為第二標准對任務進行排序?

上面的查詢解決了這個問題,但您也可以使用:

Task.order(important: :asc, created_at: :desc)

希望其中一些有用。

由OP解決。

task.rb

 class Task < ActiveRecord::Base
   belongs_to :user

   scope :important, -> { where(important: true) }
   scope :urgent, -> { where(urgent: true) }
   default_scope  { order(:important => :desc, :urgent => :desc, :updated_at => :desc, :created_at => :desc) }
 end

new.html.erb

<h1>New Task</h1>


 <%= form_for(@task) do |f| %>

   <%= f.text_field :content, placeholder: "Content", class: "formfield" %>

   <strong>Important:</strong>
   <%= f.check_box :important %>

   <strong>Urgent:</strong>
   <%= f.check_box :urgent %>

   <%= f.submit "Save Content", class: "form_button" %>
 <% end %>


<%= link_to 'Back', tasks_path %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM