2019-02-21 21:10:14 +00:00
|
|
|
# rails generate
|
|
|
|
|
|
|
|
> Generate new Rails templates in an existing project.
|
2020-04-15 17:29:07 +01:00
|
|
|
> More information: <https://guides.rubyonrails.org/command_line.html#rails-generate>.
|
2019-02-21 21:10:14 +00:00
|
|
|
|
|
|
|
- List all available generators:
|
|
|
|
|
|
|
|
`rails generate`
|
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
- Generate a new model named Post with attributes title and body:
|
2019-02-21 21:10:14 +00:00
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
`rails generate model {{Post}} {{title:string}} {{body:text}}`
|
2019-02-21 21:10:14 +00:00
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
- Generate a new controller named Posts with actions index, show, new and create:
|
2019-02-21 21:10:14 +00:00
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
`rails generate controller {{Posts}} {{index}} {{show}} {{new}} {{create}}`
|
2019-02-21 21:10:14 +00:00
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
- Generate a new migration that adds a category attribute to an existing model called Post:
|
2019-02-21 21:10:14 +00:00
|
|
|
|
2020-04-15 17:29:07 +01:00
|
|
|
`rails generate migration {{AddCategoryToPost}} {{category:string}}`
|
|
|
|
|
|
|
|
- Generate a scaffold for a model named Post, predefining the attributes title and body:
|
|
|
|
|
|
|
|
`rails generate scaffold {{Post}} {{title:string}} {{body:text}}`
|