[英]How can I create checkboxes, set checkbox values, sum them up to one task model value and sort tasks according to that value?
I'm making a to-do list with Rails. 我正在用Rails列出待办事项。 I want to have two checkboxes for every task ("Important" and "Urgent").
我希望每个任务都有两个复选框(“重要”和“紧急”)。 I want to sort those tasks according to two criteria.
我想根据两个标准对这些任务进行排序。 First criteria are the checkboxes and the second criteria should be the time where the task has been created or updated.
第一个条件是复选框,第二个条件应是创建或更新任务的时间。 So with the first criteria there are 4 priorities which the tasks should be sorted:
因此,按照第一个标准,应该对任务进行排序的优先级为4:
If there are multiple tasks with the same priority those should be sorted descending by the second criteria but within the first criteria. 如果存在多个具有相同优先级的任务,则应按第二个标准降序但在第一个标准内对它们进行排序。
I would create a migration to add an integer to the task model and then set the checkboxes to the values: "important = 2" and "urgent = 1" (unchecked value = 0). 我将创建一个迁移,以将整数添加到任务模型,然后将复选框设置为以下值:“重要= 2”和“紧急= 1”(未经检查的值= 0)。 Those checkbox values should be summed up and linked to the integer in the task model and then I would sort them in the task view.
这些复选框的值应加起来并链接到任务模型中的整数,然后在任务视图中对它们进行排序。
Questions: 问题:
index.html.erb (task view) 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 (task new view) 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 %>
tasks_controller.rb 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
I don't think that summing the checkbox values to one 'task model value' is the best solution. 我不认为将复选框值加到一个“任务模型值”是最好的解决方案。 I would add both an Urgent and an Important column to your Task model.
我会在“任务”模型中同时添加“紧急”和“重要”列。 This is far more scalable in the long run and easier to work with generally.
从长远来看,这具有更大的可伸缩性,并且通常更易于使用。 Then it's just a case using the Rails check_box_tag helper.
然后,这只是使用Rails check_box_tag帮助器的一种情况。 http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag
http://apidock.com/rails/ActionView/Helpers/FormTagHelper/check_box_tag
If you have separate columns/attributes for the important and urgent you can create scopes on your task model to sort as you wish. 如果对于重要和紧急事件有单独的列/属性,则可以在任务模型上创建范围以根据需要进行排序。
scope :important, -> { where(important: true) }
scope :urgent, -> { where(urgent: true) }
You can then chain your named scopes as you require. 然后,您可以根据需要链接命名范围。
Task.important.urgent
or maybe you can also specify a recent scope: 或者,您也可以指定最近的范围:
scope :recent, -> { order("posts.updated_at DESC") }
and then call: 然后致电:
Task.important.recent
which would run the following SQL query: 它将运行以下SQL查询:
# SELECT "tasks".* FROM "tasks" WHERE "tasks"."important" = 'true'
# ORDER BY tasks.updated_at DESC
The above query sort of addresses this but you could also use: 上面的查询解决了这个问题,但您也可以使用:
Task.order(important: :asc, created_at: :desc)
Hope some of this is helpful. 希望其中一些有用。
Solution by OP. 由OP解决。
task.rb 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 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.