Skip to content

Instantly share code, notes, and snippets.

@cdesch
Last active September 2, 2025 19:45
Show Gist options
  • Save cdesch/2f8de645cad1d83aa251c0a20b0f7097 to your computer and use it in GitHub Desktop.
Save cdesch/2f8de645cad1d83aa251c0a20b0f7097 to your computer and use it in GitHub Desktop.
Rails Generator CheatSheet

Command Line Generator Info

Reference

You can get all of this information on the command line.

rails generate with no generator name will output a list of all available generators and some information about global options. rails generate GENERATOR --help will list the options that can be passed to the specified generator.

Rails Generate Examples

Create a Resource

rails generate scaffold Post name:string title:string content:text

Generate Models

rails generate model Post title:string body:text published:boolean

Add Column to Existing Model

rails generate migration AddFieldToModel field:type

Column Types

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean

Rspec Genorators

Documentation

rails generate rspec:model widget

will create a new spec file in spec/models/widget_spec.rb

The same generator pattern is available for all specs:

scaffold
model
controller
helper
view
mailer
observer
integration
feature
job

Generating specific views

rails g rspec:view widget index edit new show
      create  spec/views/widget
      create  spec/views/widget/index.html.erb_spec.rb
      create  spec/views/widget/edit.html.erb_spec.rb
      create  spec/views/widget/new.html.erb_spec.rb
      create  spec/views/widget/show.html.erb_spec.rb
@lbraun
Copy link

lbraun commented Jun 26, 2019

Super helpful, thank you! Just a note: you've got this in there twice: https://gist.github.com/cdesch/2f8de645cad1d83aa251c0a20b0f7097#adding-a-unique-property-to-a-field

@cdesch
Copy link
Author

cdesch commented Jun 28, 2019

You're welcome @ibraun. I fixed it.

@kcamcam
Copy link

kcamcam commented Jan 21, 2021

I appreciate the time you put into making this. Thank you 🙂

@joshtune
Copy link

very helpful

@narokir
Copy link

narokir commented Apr 21, 2022

Helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment