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.
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:
luckyCLI command. All Lucky tasks are executed from this
gen.resource.browseris the name of the command to run
Fortuneis the name of our model
text:Stringis 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
--help flag. (e.g.
lucky gen.resource.browser -h)
Some shells may require the last portion to be wrapped in quotes. (i.e.
Lucky generated a migration file for us located in
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:
To execute this code, we will run the
db.migrate cli task. Enter
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.
/fortunesin your browser.
/fortunes. Notice how it asks you to sign in first?
We will fix the association issue in the next section.