A settings page for workspace members
Ruby · Rails · beginner · modification
Adds a settings page where a workspace member edits their own name, email and time zone. `config/routes.rb` adds a singular `resource :settings` with `edit` and `update`. The new `SettingsController` loads the signed-in member from `Current.member`, so there is no id in the URL, and on `update` saves the strong-params subset and redirects back with a notice, or re-renders the form with a 422. `settings/edit.html.erb` is the form (a `time_zone_select` for the zone). `settings_controller_test.rb` adds integration tests for the page and its save and validation paths.
Workspace app on Ruby 4.0 and Rails 8.1, SQLite in development and test (nothing here depends on the database engine), 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` (and `account`, the member's account). Every controller action runs behind it unless it skips it. `Member` belongs to an `Account` and has `name` (string, at most 80 characters, required), `email` (normalized with `strip.downcase`, a valid address, unique across the app by validation and a unique index, used to sign in), `time_zone` (a name from `ActiveSupport::TimeZone.all`, default `UTC`) and `role`, a string enum `member` / `admin` / `owner` (default `member`). Admins and owners get the admin pages from `role`, read on every request. There is no email confirmation flow in this app: a member's email changes directly. Two members saving the same new email at the same instant is out of scope. `Admin::MembersController` (owners only) is where roles change. The test helper `sign_in_as(member)` signs the integration session in as that member. Forgery protection is on outside tests, and the layout renders `csrf_meta_tags`.
Requirements
- `GET /settings/edit` (new) shows the signed-in member a form with three of their own fields: `name`, `email` and `time_zone`. `PATCH /settings` (new) saves the submitted subset of those three fields on the signed-in member's own record and redirects back to the form (303) with the notice `Settings saved.`; a field that is not submitted keeps its value.
- Validation is the existing `Member` validations: on a violation the form is rendered again with the error messages and status 422, and nothing is saved. A request without a `member` parameter answers 400 (as does a submission with no editable field).
- Only owners may change a member's `role`, and they do it on the existing owners' page (`Admin::MembersController`, unchanged). `PATCH /settings` is open to every signed-in member, including plain members, and must never change `role`: a `role` value in the submission is ignored.
- Signed-out visitors are redirected to `new_session_path`. Unchanged: the owners' page and the sign-in flow.
Files touched
- config/routes.rb
- app/controllers/settings_controller.rb
- app/views/settings/edit.html.erb
- test/controllers/settings_controller_test.rb