In a word, a command that creates all the necessary files and completes the routing including the DB.
$rails g scaffold model name column name 1:Data type column name 2:Data type....
model Migration file controller View routing
If you generate a name column age column
with a model name of ʻusersinfo`.
$rails g scaffold usersinfo name:string age:integer
Since there is no special description, it is omitted.
*******_*******.rb
class CreateUsersinfos < ActiveRecord::Migration[5.1]
def change
create_table :usersinfos do |t|
t.string :name #Describes the column name and data type specified at the time of generation
t.integer :age
t.timestamps
end
end
end
Described so far at the time of generation
*******_controller.rb
class UsersinfosController < ApplicationController
before_action :set_usersinfo, only: [:show, :edit, :update, :destroy]
# GET /usersinfos
# GET /usersinfos.json
def index
@usersinfos = Usersinfo.all
end
# GET /usersinfos/1
# GET /usersinfos/1.json
def show
end
# GET /usersinfos/new
def new
@usersinfo = Usersinfo.new
end
# GET /usersinfos/1/edit
def edit
end
# POST /usersinfos
# POST /usersinfos.json
def create
@usersinfo = Usersinfo.new(usersinfo_params)
respond_to do |format|
if @usersinfo.save
format.html { redirect_to @usersinfo, notice: 'Usersinfo was successfully created.' }
format.json { render :show, status: :created, location: @usersinfo }
else
format.html { render :new }
format.json { render json: @usersinfo.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /usersinfos/1
# PATCH/PUT /usersinfos/1.json
def update
respond_to do |format|
if @usersinfo.update(usersinfo_params)
format.html { redirect_to @usersinfo, notice: 'Usersinfo was successfully updated.' }
format.json { render :show, status: :ok, location: @usersinfo }
else
format.html { render :edit }
format.json { render json: @usersinfo.errors, status: :unprocessable_entity }
end
end
end
# DELETE /usersinfos/1
# DELETE /usersinfos/1.json
def destroy
@usersinfo.destroy
respond_to do |format|
format.html { redirect_to usersinfos_url, notice: 'Usersinfo was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_usersinfo
@usersinfo = Usersinfo.find(params[:id])
end
# Only allow a list of trusted parameters through.
def usersinfo_params
params.require(:usersinfo).permit(:name, :age)
end
end
All views described in the controller are automatically generated.
You have to write the routing yourself.
routes.rb
resources :View name#Write by yourself
Rails has a function to check the input contents, and scaffold is generated privately in the controller.
If you add a column to after executing
scaffold`, it will not be saved in the DB unless you add the column name in this private method.
***.controller.rb
private
def usersinfo_params
params.require(:usersinfo).permit(:name, :age, :location, :description, :certification, :note)
#In the permit, the column described when scaffold is executed(:name, :age)Since only is described, it is necessary to add the added column.
end
Recommended Posts