Lucky comes with a lot of neat commands to make building your application a little easier.
From your terminal, type lucky -h
. Give it a moment to compile, then see a list of the commands you can use.
For our next section, we will be generating a new “resource”; the “Fortune”. In this context, a resource is a Model, Action, Page, Component, Query, and Operation. Lucky breaks a resource down in to smaller class components which we will discuss over the tutorial.
For more information on Lucky CLI commands, read the Built In Tasks guide.
Before we generate the new resource, we should plan out what this model will need.
A User
can write as many “fortunes” as they want, and each Fortune
will belong to that user.
The fortunes themselves will be a short bit of text. Pretty simple!
Let’s run our generator CLI task. Enter this command in your terminal:
lucky gen.resource.browser Fortune text:String
The command breaks down like this:
lucky
CLI command. All Lucky tasks are executed from thisgen.resource.browser
is the name of the command to runFortune
is the name of our modeltext:String
is the name of the column and its type separated with a colon. You can add as many as you need here, just separate them by a space.You can learn more about each CLI command by passing the -h
or --help
flag. (e.g. lucky gen.resource.browser -h
)
Some shells may require the last portion to be wrapped in quotes. (i.e.
"text:String"
)
Lucky generated a migration file for us located in db/migrations/20241221130147_create_fortunes.cr
.
This migration file will generate a SQL statement for us that will create our “fortunes” table and add the columns our
table needs like text
, as well as a few other columns Avram gives to us for free: id
, created_at
, and updated_at
.
To execute this code, we will run the db.migrate
CLI task. Enter lucky db.migrate
:
lucky db.migrate
You should see a response that says “Migrated”.
For more information on migrations, read the Migrations guide.
Now that we’ve updated our database, we can boot our app to test a few things.
Try this:
lucky dev
)/fortunes
in your browser./fortunes
. Notice how it asks you to sign in first?We will fix the association issue in the next section.