Rails Generators = “Magic”

Rails Generators make the foundation of any Ruby on Rails Project a breeze.

As a relatively new software engineering student, staring at blank text editor was more than intimidating ,the question “where do i even start?” would creep into head right away and i would scurry to look back at notes or documentation on how to begin a my project.

This “where do i start” feeling would follow me into the next phase of my coding journey and then I met Ruby on Rails. Ruby on Rails is a Ruby Framework that truly holds any new developers hand while building a new application from the ground up. Ruby On Rails uses a MVC pattern to give any Ruby project a strong base.

MVC stands for Model View Controller, the Ruby on Rails framework follows this pattern of organizing code. The Model part of MVC deals with the data. The View deals with what the user will interact with and controller, mainly deals with the logic. You should look at the controller as the coordinator. When a user sends a request in a web page, the controller goes to the model and asks for the data first, then goes to the view and asks for the view and finally sends all of the information back to the user.

“Learning to build a modern web application is daunting. Ruby on Rails makes it much easier and more fun. It includes everything you need to build fantastic applications” -rubyonrails.org

Rails G Magic

Ruby continues to make building the foundation for any Rails application as simple as could be with Rails Generators , with a simple line of code you can have one or all three parts of your MVC pattern and even more!, Model , Resource and Scaffold are all Rails Generators that are used to help any programmer start off on the right foot with there Rails project.

Rails G Model

Entering

rails g model Sample username

in your command line will generate the following:

**A model file sample.rb in your models folder:

class Sample < ActiveRecord::Baseend

**A migration file timestamp_sample.rb in your db/migrate folder:

class CreateSamples < ActiveRecord::Migration  def change 
create_table :samples do |t|
t.string :username

t.timestamps
end
end
end

Rails G Resource

Entering

rails g resource Sample username

in your command line will generate the following:

**A model file sample.rb in your models folder:

class Sample < ActiveRecord::Base
end

**A migration file timestamp_sample.rb in your db/migrate folder:

class CreateSamples < ActiveRecord::Migration
def change
create_table :samples do |t|
t.string :username
t.timestamps
end
end
end

**An empty samples_controller.rb file in your controllers folder

class SamplesController < ApplicationController
end

**7 RESTful routes in your routes.rb file will automatically be generated

resources :samples 

Rails G Scaffold

Entering

rails g scaffold Sample username

in your command line will generate the following

*A model file sample.rb in your models folder

class Sample< ActiveRecord::Base
end

*A migration file timestamp_samples.rb in your db/migrate folder

class CreateSamples < ActiveRecord::Migration
def change
create_table :samples do |t|
t.string :username
t.timestamps
end
end
end

*A samples_controller.rb file in your controllers directory. When a scaffold is generated, seven public methods and two private methods will be added to your controller

class SamplesController < ApplicationController
before_action :set_sample, only: [:show, :edit, :update, :destroy]
# GET /samples
# GET /samples.json
def index
@samples = Sample.all
end
# GET /samples/1
# GET /samples/1.json
def show
end
# GET /samples/new
def new
@sample = Sample.new
end
# GET /samples/1/edit
def edit
end
# POST /samples
# POST /samples.json
def create
@sample = Sample.new(sample_params)
respond_to do |format|
if @sample.save
format.html { redirect_to @sample, notice: 'Sample was successfully created.' }
format.json { render action: 'show', status: :created, location: @sample }
else
format.html { render action: 'new' }
format.json { render json: @sample.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /samples/1
# PATCH/PUT /samples/1.json
def update
respond_to do |format|
if @sample.update(sample_params)
format.html { redirect_to @sample, notice: 'Sample was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @sample.errors, status: :unprocessable_entity }
end
end
end
# DELETE /samples/1
# DELETE /samples/1.json
def destroy
@sample.destroy
respond_to do |format|
format.html { redirect_to samples_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_sample
@sample = Sample.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def sample_params
params.require(:sample).permit(:username)
end
end

7 RESTful routes in your routes.rb file will automatically be generated

resources :samples

Seven corresponding view files in your views directory:

_form.html.erb,

edit.html.erb,

index.html.erb,

index.json.jbuilder,

new.html.erb,

show.html.erb

show.json.jbuilder.

Each view will contain html and embedded ruby.

Rails G Model, Resource and Scaffold are extraordinarily helpful ways that the Ruby on Rails Framework helps developers set the foundation in a time saving manner for any Rails Project.

Full-Stack Software Engineer / New York City / www.danielcreyes.dev