DEV Community

Cover image for TLDR: Button to update status attribute of a table
Yaroslav Shmarov
Yaroslav Shmarov

Posted on • Originally published at blog.corsego.com on

4 1

TLDR: Button to update status attribute of a table

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!

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay