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:
Post a Comment