RAILS GENERATORS
Let's talk generators in Rails. They save us time, AND they provide us with some awesome benefits:
They can set up some basic specs for an application's test suite. They won't write our complex logic tests for us, but they will provide some basic examples.
They are set up to work the same way each time. This helps standardize your code and enables your development to be more efficient since you don't have to worry as much about bugs related to spelling, syntax errors, or anything else that can occur when writing code manually.
They follow Rails best practices, which includes utilizing RESTful naming patterns, removing duplicate code, using partials and a number of other best of breed design patterns. (If you don't know what all of these are, don't worry — we will cover them shortly.)
SOURCE: LEARN.CO
FUN FACT - when you're creating an application with rails
, you're actually already using a Rails generator.
Once you've created a Rails application, you can show a list of all available generators by running:
bin/rails generate
OR bin/rails generate helper --help
(The latter will give you detailed descriptions)
CREATING GENERATORS
Entering the basic command into the terminal will follow syntax like this:
rails g <generator name> <options> --no-test-framework
There are many generators, but the 4 we'll focus on here are:
- Migrations
- Models
- Controllers
- Resources
1. Model
- Model will create a model & migration
rails g model Pet name breed
This would generate a Pet model, and a migration to create a pets table with two columns, name and breed. String is the default input for a table column, so we don't need to put name:string when using the generator
2. Controller
- Controller will create a controller
rails g controller admin dashboard stats financials settings --no-test-framework
This will generate a controller file that inherits from ApllicationController, a set of routes to each of the arguments/options (dashboard, stats, financials, settings), a new directory for all of the view templates along with a view template file for each of the controller actions that we declared in the generator command, a view helper method file, a Coffeescript file for specific JavaScripts for that controller & an scss file for the styles for the controller
3. Migration
- Migration will create a migration
rails g migration add_published_status_to_posts published_status:string --no-test-framework
This would generate a migration that looks something like this:
class AddPublishedStatusToPosts < ActiveRecord::Migration
def change
add_column :posts, :published_status, :string
end
end
4. Resource
- Resource will create a model, controller, migration & views directory
rails g resource Account name payment_status --no-test-framework
This would generate a migration file with the passed atrributes, a model file that inherits from ApplicationRecord, a controller file inheriting from ApplicationController, a view directory (with no view template files), a view helper, a Coffeescript file for specific JavaScripts for that controller & an scss file for the styles for the controller and a full resources call in the routes.rb file
The last item is a MAGIC ROUTE! It shows the full set of RESTful routes that'll you'll need to perform CRUD. If you run rake routes
(optional filter) you'll see something like this:
rake routes | this account
accounts GET /accounts(.:format) accounts#index
POST /accounts(.:format) accounts#create
new_account GET /accounts/new(.:format) accounts#new
edit_account GET /accounts/:id/edit(.:format) accounts#edit
account GET /accounts/:id(.:format) accounts#show
PATCH /accounts/:id(.:format) accounts#update
PUT /accounts/:id(.:format) accounts#update
DELETE /accounts/:id(.:format) accounts#destroy
Sources: Learn.co Rails Guide
Top comments (0)