Keith Schacht’s Weblog

Subscribe
Atom feed for Rails

2 items tagged “Rails”

💡 TIL: Rails migrations can include an up_only part #

When writing a migration in Rails and you need to do one thing which can’t be automatically reversed, I always convert a def change to up and down. It turns out you can slip in an up_only block:

class AddFieldsToUsers < ActiveRecord::Migration[7.1]
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string

    up_only do
      User.delete_all
    end
  end
end
from RubyCademy

📝 Using Hotwire it was simple to build inline title editing #

After generating the Rails scaffolding for my “Conversation” model, it was just a few steps to do inline editing:

  1. Modify conversations/_conversation.html.erb to wrap it in a turbo-frame
  2. Modify conversations/_form.html.erb partial to wrap it in the same turbo-frame so that the form will replace the displayed title
  3. Add a tiny bit of stimulus to enable submit on enter/blur and to enable discard with Escape
Read more ▾