Forum members pick a public username
Ruby · Rails · intermediate · modification
First step towards public profiles: members can pick a username, shown next to their posts. The migration adds a nullable `username` column to `members` with a unique index. `Member` trims the value (an empty one becomes `nil`) and validates the format and uniqueness, leaving members without a username alone. `UsernamesController#update` (new, `PATCH /username`, routed as a singular resource) saves the signed-in member's username through strong params and answers JSON: the saved value, or the validation errors with 422. `usernames_controller_test.rb` (new) adds integration tests for the endpoint. Profile pages come in the next PR.
Cooking community forum on Ruby 4.0 and Rails 8.1, SQLite in development, test and production (one database file on a persistent volume), Minitest integration tests. Existing and unchanged (not shown): `ApplicationController` includes the `Authentication` concern, whose `before_action :require_authentication` sets `Current.member` to the signed-in `Member` or redirects to `new_session_path`; `Current` is an `ActiveSupport::CurrentAttributes` with `member`. `Member` has `name` (required, at most 80 characters) and `email` (normalized with `strip.downcase`, unique by validation and a unique index); the table has about 40,000 rows, none with a username yet. The forum's pages call `PATCH /username` with `fetch` and send the `X-CSRF-Token` header from `csrf_meta_tags`; forgery protection is on outside tests. Two members claiming the same username at the same instant is out of scope. This PR and its migration have not been deployed yet. `db/schema.rb` is regenerated from the migration and not shown. Profile pages (`/@username`, next PR) will look a username up regardless of letter case, per requirement 2. The test helper `sign_in_as(member)` signs the integration session in as that member.
Requirements
- `PATCH /username` (new) with a JSON body `{"member": {"username": ...}}` sets or changes the signed-in member's public username and answers 200 `{"username": <saved value>}`. Surrounding whitespace is trimmed; a username is 3 to 20 ASCII letters, digits or underscores. An empty string or `null` removes the member's username (200 with `{"username": null}`). Any other string answers 422 `{"errors": {"username": [...]}}` and saves nothing. A request whose `member` is missing or not an object, or whose `username` is missing, an array or an object, answers 400; a number or boolean `username` is converted to a string first and then validated like any other (`12345` is saved as `"12345"`, while `true` and `1.5` fail the format check).
- One username per member, regardless of letter case: `AnaCooks`, `anacooks` and `ANACOOKS` are the same username. Asking for a username another member already has, in any letter case, answers 422 with the error `has already been taken` and saves nothing. A member may change the letter case of their own username.
- The username is stored and shown the way the member typed it (after trimming): the forum displays `@AnaCooks`, not `@anacooks`.
- The database enforces requirement 2 too: a row written without the model's validations (a console data fix, `update_column`, a bulk import) cannot give a second member the same username in another letter case. Members who have not picked a username keep `NULL` and never conflict with each other.
- Signed-out requests are redirected to `new_session_path`, as for every other action. Unchanged: the sign-in flow and the topics pages.
Files touched
- db/migrate/20261103090000_add_username_to_members.rb
- app/models/member.rb
- app/controllers/usernames_controller.rb
- config/routes.rb
- test/controllers/usernames_controller_test.rb