generate-components
Use this skill when generating Hanami 2.x components via the CLI.
Core principle: Generators follow naming conventions. The command name determines the file path and class name.
Quick Reference
| Command | Generates | Path |
|---|---|---|
hanami generate action <name> |
Action class | app/actions/{path}.rb |
hanami generate view <name> |
View class + template | app/views/{path}.rb + app/templates/{path}.html.erb |
hanami generate slice <name> |
Slice directory structure | slices/{name}/ |
hanami generate migration <name> |
Migration file | db/migrate/{timestamp}_{name}.rb |
hanami generate relation <name> |
Relation class | app/relations/{name}.rb |
hanami generate repo <name> |
Repository class | app/repos/{name}.rb |
hanami generate entity <name> |
Entity class | app/entities/{name}.rb |
Core Rules
Generate an Action:
hanami generate action users.indexGenerates:
app/actions/users/index.rb- Class:
MyApp::Actions::Users::Index
With a Slice:
hanami generate action api.users.indexGenerates:
slices/api/actions/users/index.rb- Class:
MyApp::Slices::Api::Actions::Users::Index
Generate a View:
hanami generate view users.indexGenerates:
app/views/users/index.rbapp/templates/users/index.html.erb
Generate a Slice:
hanami generate slice apiGenerates:
slices/api/config/routes.rbslices/api/config/slice.rbslices/api/actions/slices/api/views/slices/api/templates/
Generate a migration:
hanami generate migration create_usersGenerates:
db/migrate/{timestamp}_create_users.rb
Always verify generated files: Generators will fail if target files already exist. After generating, confirm the expected files are present and correctly named:
# Confirm the action file exists ls app/actions/users/index.rb # Confirm the view and template were created together ls app/views/users/index.rb app/templates/users/index.html.erb # Confirm class name matches module nesting by grepping the generated file grep 'class Index' app/actions/users/index.rb # Expected: class Index < MyApp::Action (or equivalent)Key checks:
- File paths match the expected pattern.
- Class names align with module nesting (e.g.
users.index→MyApp::Actions::Users::Index). - Slices are correctly prefixed:
<slice>.<resource>.<action>(omitting this places components in the defaultapp/folder).
Common Mistakes & Troubleshooting
| Problem / Mistake | Resolution |
|---|---|
| Using underscores instead of dots | CLI commands require dot notation: users.index, not users_index. |
| Omitting the slice prefix | Use api.users.index to generate in the api slice. |
| Singular names for Actions | Actions should use plural resource names: users.index, not user.index. |
| File conflicts / stale files | Generators never overwrite. Manually delete conflicting files before re-generating. |
Integration
| Related Skill | When to chain |
|---|---|
create-app (../create-app/SKILL.md) |
Generators are used after creating the app. |
create-action (../create-action/SKILL.md) |
Generated Actions need to be filled with logic. |
create-view (../create-view/SKILL.md) |
Generated Views need exposures defined. |
create-slice (../create-slice/SKILL.md) |
Generated Slices need routes and configuration. |
write-migration (../write-migration/SKILL.md) |
Generated migrations need schema definitions. |