Tuesday, February 7, 2017

What happens when you generate a scaffold in Rails 5 ?

Q. What happens when you rails g scaffold Employee eno:string ename:string dept:string date_join:date active:boolean ?

ANSWER

1. Migration file created as yyyymmddhhmmss_create_employees.rb

CreateEmployees < ActiveRecord::Migration[ 5.0]

  def change    
    create_table :employees do |t|
      t.string :eno      
      t.string :ename      
      t.string :dept      
      t.date :date_join      
      t.boolean :active
      t.timestamps
    end  
  end
end

2. Employee Model created as employee.rb

class Employee < ApplicationRecord
end

3. Employees Controller created as employees_controller.rb


class EmployeesController < ApplicationController  
  before_action :set_employee, only: [:show, :edit, :update, :destroy]

  # GET /employees  # GET /employees.json  
  def index    
   @employees = Employee.all
  end

  # GET /employees/1  # GET /employees/1.json  
  def show  
  end
 
  # GET /employees/new    
  def new    
     @employee = Employee.new
  end

  # GET /employees/1/edit  
  def edit  
  end
  
  # POST /employees  # POST /employees.json  
  def create    
   @employee = Employee.new(employee_params)
    respond_to do |format|
      if @employee.save
        format.html { redirect_to @employee, notice: 'Employee was successfully created.' }
        format.json { render :show, status: :created, location: @employee }
      else        
        format.html { render :new }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end    end  end

  # PATCH/PUT /employees/1  # PATCH/PUT /employees/1.json  
   def update    
      respond_to do |format|
      if @employee.update(employee_params)
        format.html { redirect_to @employee, notice: 'Employee was successfully updated.' }
        format.json { render :show, status: :ok, location: @employee }
      else        
        format.html { render :edit }
        format.json { render json: @employee.errors, status: :unprocessable_entity }
      end    end  end
  
 # DELETE /employees/1  # DELETE /employees/1.json  
   def destroy    @employee.destroy
    respond_to do |format|
      format.html { redirect_to employees_url, notice: 'Employee was successfully destroyed.' }
      format.json { head :no_content }
    end  
  end
  
 private    
   # Use callbacks to share common setup or constraints between actions.    
   def set_employee      
     @employee = Employee.find(params[:id])
    end
   
   # Never trust parameters from the scary internet, only allow the white list through.    
   def employee_params      
     params.require(:employee).permit(:eno, :ename, :dept, :date_join, :active)
   end
  end

4. Views created under Views/employees folder

_form.html.erb, edit.html.erb, new.html.erb and show.html.erb automatically created

5. Tests folder

Test files for controllers employees_controller_tests.rb Test files for models employees_tests.rb Test files for fixtures employees.yml

6. Routes: resources:employees

No comments:

Welcome to Rails.. Rails... Rails !

In 1995, I started the popular Clipper...Clipper... Clipper website (no blogs then) which was very popular and linked by virtually every Clipper-related site. When I switched to Windows via Delphi in 1997, I started the Delphi... Delphi... Delphi site. In June 2007, I discovered Ruby on Rails and no prize for guessing what I am gonna name this blog. which I started on 2nd October 2007.

As at 10th June 2010, we have 13,364 unique visitors from more than 84 countries such as Angola, Andorra, Argentina, Australia, Austria, Algeria,Barbados, Bosnia and Herzogovina, Belgium, Brazil, Bulgaria, Bangladesh, Belarus, Bolivia, Chile, Cambodia, Cape Vede, Canada, China, Colombia, Costa Rica, Croatia, Cyprus, Czech Republic, Denmark, Egypt, Estonia, Finland, France, Guadeloupe, Guatemala, Germany, Greece, Hong Kong, Hungary, India, Indonesia, Ireland, Israel, Italy, Japan, Kenya, Korea, Lithuania, Latvia, Malaysia, Mexico, Macao, Netherlands, Nepal, Norway, New Zealand, Oman, Panama, Peru, Poland, Portugal,Paraguay , Philippines, Romania, Russian Federation, Saudi Arabia, Singapore, Spain, Slovakia, Slovenia, Serbia, South Korea, Slovenia, South Africa, Spain, Switzerland, Sri Lanka, Sweden, Taiwan, Thailand, Turkey, United Arab Emirates, Ukraine, USA, UK, Venezuela, Vietnam

CCH
10th June 2010, 19:42