There is a list of tasks (Task class instances).
Each task has a priority ( integer type field).
On the html page you need to click on the buttons "up" "down" to increase / decrease the priority of the task.

I wanted to do something like this (increase the priority by clicking on %i.fa.fa-caret-up )

 = link_to task, remote: true, method: :put do %i.fa.fa-caret-up 

But it's not right.

There was also a council to make a TaskPrioritiesController controller, but I haven’t figured out that yet, and I don’t understand how this controller will work.

Actually, the question is, how do I properly organize a change in the priority parameter of an instance of the Task class?

ps For the literature on this topic will also be grateful

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

2 answers 2

References:

 = link_to 'Увеличить', up_task_path(task), remote: true = link_to 'Уменьшить', down_task_path(task), remote: true 

In routes.rb:

 resources :tasks do member do get :up get :down end end 

In the controller:

 class TasksController < ApplicationController before_action :set_task, only: [:up, :down] def up @task.up! # Реализация внутри модели end def down @task.down! # Реализация внутри модели end private def set_task @task = Task.find(params[:id]) end end 

Here I once wrote about member .

  • I think it will help. but it is not a CRUD way, so to speak. - Mark Osipenko
  • And if in your variant try using the PATCH method instead of the PUT method? - MAXOPKA

There are many ifs here.

In Rails, it is customary to implement an update action through the update method of the model. If you did this, then you may not need any changes on the server at all.

If the priority increase consists only of assigning a new priority value (and the rest is in callbacks or is not required), then it is enough just to call an already existing update action with a set of parameters containing the new priority value and only that one .

That is, conditionally, you need to make a call to TasksController#update with the PATCH verb with parameters like these:

 { task: { priority: 42 } } 

And the new priority value is written into the task.

If you want to be limited to CRUD, then you have no other options. And this option will be quite "fun" to support , if your priorities should be a continuous sequence of numbers in the manner of 1 , 2 , 3 , 4 ...


The question remains of how you were going to update the DOM, shift the task, but that's another story ...