LinuxCommandLibrary

rails

Manage Ruby on Rails applications

TLDR

Create a new rails project

$ rails new "[project_name]"
copy

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

Run migrations
$ rails db:migrate
copy

List all routes
$ rails routes
copy

Start local server for current project on port 3000
$ rails server
copy

Start local server for current project on a specified port
$ rails server [[-p|--port]] "[port]"
copy

Open console to interact with application from command-line
$ rails console
copy

Check current version of rails
$ rails [[-v|--version]]
copy

SYNOPSIS

rails command [options]

PARAMETERS

new app_name
    Creates a new Rails application in a directory named app_name.

generate generator_name [args]
    Generates code for various Rails components (e.g., model, controller, migration).

server
    Starts the Rails web server.

console
    Starts the Rails console, an interactive environment for running Ruby code within the Rails application.

db:migrate
    Runs pending database migrations.

routes
    Lists all defined routes in the application.

test
    Runs all tests in the application.

destroy generator_name [args]
    Reverts files that was generated by rails generate.

-v, --version
    Shows the Rails version.

-h, --help
    Shows help information for the `rails` command and its subcommands.

DESCRIPTION

The rails command is the primary interface for creating and managing Ruby on Rails applications. It provides various subcommands and options to generate new projects, manage databases, run servers, and perform other essential tasks related to Rails development.

It streamlines the development process by automating repetitive tasks and providing a consistent structure for Rails applications. The command is used to create new Rails projects with a basic directory structure, including folders for models, views, controllers, and configurations. It also generates files for database migrations, testing, and other common components.

Through the use of subcommands such as `generate`, `server`, `console`, `db:migrate`, and many others, developers can interact with their Rails application and its environment. The rails command is typically used in conjunction with other tools and gems within the Ruby ecosystem to build and deploy web applications. Its extensive suite of utilities makes it indispensable for Rails developers of all skill levels.

CAVEATS

Requires Ruby and RubyGems to be installed. Relies on a correctly configured environment for database connections and other dependencies. Understanding the specific Rails version used is essential as syntax and options can vary.

ENVIRONMENT VARIABLES

The behavior of the rails command can be influenced by environment variables such as `RAILS_ENV` (which specifies the environment, e.g., development, production, test), `DATABASE_URL` (specifies the database connection string), and others related to specific gems or services used by the application.

GEMS

Rails relies heavily on RubyGems. The `Gemfile` in a Rails application lists the required gems. The bundle command is commonly used to manage these gems and their dependencies. The rails command often invokes functionality provided by these gems.

CONFIGURATION

The `config` directory in a Rails application contains configuration files for various aspects of the application, such as database settings, routes, and environment-specific settings. The rails command often interacts with these configuration files during tasks like database migrations and server startup.

HISTORY

The rails command was introduced with the Ruby on Rails framework, which was created by David Heinemeier Hansson and first released in 2004. It aimed to provide a more productive and convention-over-configuration approach to web development. The rails command evolved along with the framework, gaining new features and subcommands to support different aspects of Rails development. Its core purpose has always been to simplify the process of creating, managing, and deploying Rails applications.

SEE ALSO

ruby(1), gem(1), rake(1)

Copied to clipboard