HiveBrain v1.2.0
Get Started
← Back to all entries
snippetbashTip

rails generate — Generate new Rails templates in an existing project. More information: <https://guides.rubyonrails.o

Submitted by: @import:tldr-pages··
0
Viewed 0 times
railscommandrails generatenewcliexistingtemplatesgenerate

Problem

How to use the rails generate command: Generate new Rails templates in an existing project. More information: <https://guides.rubyonrails.org/command_line.html#bin-rails-generate>.

Solution

rails generate — Generate new Rails templates in an existing project. More information: <https://guides.rubyonrails.org/command_line.html#bin-rails-generate>.

List all available generators:
rails generate


Generate a new model named Post with attributes title and body:
rails generate model {{Post}} {{title:string}} {{body:text}}


Generate a new controller named Posts with actions index, show, new, and create:
rails generate controller {{Posts}} {{index}} {{show}} {{new}} {{create}}


Generate a new migration that adds a category attribute to an existing model called Post:
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}}

Code Snippets

List all available generators

rails generate

Generate a new model named Post with attributes title and body

rails generate model {{Post}} {{title:string}} {{body:text}}

Generate a new controller named Posts with actions index, show, new, and create

rails generate controller {{Posts}} {{index}} {{show}} {{new}} {{create}}

Generate a new migration that adds a category attribute to an existing model called Post

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}}

Context

tldr-pages: common/rails generate

Revisions (0)

No revisions yet.