LinuxCommandLibrary

rails-generate

Generate new Rails application components

TLDR

List all available generators

$ rails generate
copy

Generate a new model named Post with attributes title and body
$ rails generate model [Post] [title:string] [body:text]
copy

Generate a new controller named Posts with actions index, show, new and create
$ rails generate controller [Posts] [index] [show] [new] [create]
copy

Generate a new migration that adds a category attribute to an existing model called Post
$ rails generate migration [AddCategoryToPost] [category:string]
copy

Generate a scaffold for a model named Post, predefining the attributes title and body
$ rails generate scaffold [Post] [title:string] [body:text]
copy

SYNOPSIS

rails generate GENERATOR [options]

PARAMETERS

GENERATOR
    The name of the generator to invoke (e.g., model, controller, scaffold).

-h, [--help]
    Show help message and exit.

-p, [--pretend]
    Run but do not make any changes.

-f, [--force]
    Overwrite files that already exist.

-s, [--skip]
    Skip files that already exist.

-q, [--quiet]
    Suppress status output.

-t, [--template=PATH]
    Specify a custom template.

--assets
    Generate assets (stylesheets, javascripts) when creating a resource or scaffold.

--no-assets
    Do not generate assets (stylesheets, javascripts) when creating a resource or scaffold.

--api
    Generate API only application (no views).

DESCRIPTION

The rails-generate command is a powerful tool within the Ruby on Rails framework, used for automating the creation of various components of a Rails application.
It streamlines development by generating boilerplate code for models, controllers, views, migrations, and other essential elements. This reduces repetitive coding and ensures consistency across the application.
It uses templates to generate files and helps follow the convention over configuration principle.
The command takes a generator name as an argument, along with optional parameters to customize the generated code. These generators include common elements like models, controllers, and scaffolding, as well as resources, mailers, and more. Custom generators can also be created to tailor the command to specific project needs. The rails-generate command significantly accelerates the development process by automating the creation of common application components and minimizing boilerplate code.

CAVEATS

Generated code might require manual adjustments to fully meet application requirements.
Understanding the underlying Rails conventions is crucial for effective use.

GENERATOR EXAMPLES

rails generate model User name:string email:string - Generates a User model with name and email attributes.
rails generate controller Articles index show new create edit update destroy - Generates an Articles controller with specified actions.
rails generate scaffold Post title:string content:text - Generates a complete scaffold for managing Posts, including model, controller, and views.

CUSTOM GENERATORS

Rails allows the creation of custom generators to automate tasks specific to a project.
Custom generators are created by placing files in the lib/generators directory of your Rails application.

HISTORY

The rails-generate command has been a cornerstone of Ruby on Rails development since its early versions. It was introduced to automate the creation of application components, promoting the DRY (Don't Repeat Yourself) principle. Over time, it has evolved to support a wider range of generators and options, reflecting the expanding capabilities of the Rails framework and the needs of its developers.
The Rails community has also contributed by creating custom generators, further extending the functionality of this important tool.

SEE ALSO

rails(1), rails-dbconsole(1), rails-server(1)

Copied to clipboard