TLDR: Button to update status attribute of a table

Mission: add buttons to change the status of a task

HOWTO:

migration – add status column to tasks

add_column :tasks, :status, :string, null: false, default: “planned”

Enter fullscreen mode

Exit fullscreen m…


This content originally appeared on DEV Community and was authored by Yaroslav Shmarov

Mission: add buttons to change the status of a task

change-status.gif

HOWTO:

migration - add status column to tasks

add_column :tasks, :status, :string, null: false, default: "planned"

Enter fullscreen mode Exit fullscreen mode

task.rb - list available statuses

  validates :status, presence: true
  STATUSES = [:planned, :progress, :done]

Enter fullscreen mode Exit fullscreen mode

tasks_controller.rb - add action to change status

  def change_status
    @task = Task.find(params[:id])
    if params[:status].present? && Task::STATUSES.include?(params[:status].to_sym)
      @task.update(status: params[:status])
    end
    redirect_to @task, notice: "Status updated to #{@task.status}"
  end

Enter fullscreen mode Exit fullscreen mode

routes.rb - add actionable link to change status.

  resources :tasks do
    member do
      patch :change_status
    end
  end

Enter fullscreen mode Exit fullscreen mode

tasks/show.html.erb

  <% Task::STATUSES.each do |status| %>
    <%= link_to change_status_task_path(@task, status: status), method: :patch do %>
      <%= status %>
    <% end %>
  <% end %>

Enter fullscreen mode Exit fullscreen mode

Voila, that's it!


This content originally appeared on DEV Community and was authored by Yaroslav Shmarov


Print Share Comment Cite Upload Translate Updates
APA

Yaroslav Shmarov | Sciencx (2021-02-15T14:24:53+00:00) TLDR: Button to update status attribute of a table. Retrieved from https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/

MLA
" » TLDR: Button to update status attribute of a table." Yaroslav Shmarov | Sciencx - Monday February 15, 2021, https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/
HARVARD
Yaroslav Shmarov | Sciencx Monday February 15, 2021 » TLDR: Button to update status attribute of a table., viewed ,<https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/>
VANCOUVER
Yaroslav Shmarov | Sciencx - » TLDR: Button to update status attribute of a table. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/
CHICAGO
" » TLDR: Button to update status attribute of a table." Yaroslav Shmarov | Sciencx - Accessed . https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/
IEEE
" » TLDR: Button to update status attribute of a table." Yaroslav Shmarov | Sciencx [Online]. Available: https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/. [Accessed: ]
rf:citation
» TLDR: Button to update status attribute of a table | Yaroslav Shmarov | Sciencx | https://www.scien.cx/2021/02/15/tldr-button-to-update-status-attribute-of-a-table/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.