[RUBY] How to use rails g scaffold, functions, precautions

What is scaffold?

In a word, a command that creates all the necessary files and completes the routing including the DB.

Generation method

$rails g scaffold model name column name 1:Data type column name 2:Data type....

Generated file

model Migration file controller View routing

Contents of the generated file

If you generate a name column age column with a model name of ʻusersinfo`.

$rails g scaffold usersinfo name:string age:integer

model

Since there is no special description, it is omitted.

Migration file

*******_*******.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

controller

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

View

All views described in the controller are automatically generated.

routing

You have to write the routing yourself.

routes.rb


resources :View name#Write by yourself

important point

Addition to params.require

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

How to use rails g scaffold, functions, precautions
[Rails] How to use enum
[Rails] How to use enum
How to use rails join
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
[Rails] How to use gem "devise"
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to use Ruby on Rails
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
How to use MySQL in Rails tutorial
[Ruby on Rails] How to use redirect_to
[Note] How to use Rails 6 Devise + cancancan
[Ruby on Rails] How to use kaminari
[Rails] How to use video_tag to display videos
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
[Rails] How to use select boxes in Ransack
How to use map
How to use collection_select
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to uninstall Rails
[How to use label]
How to use identity
How to use hashes
How to use JUnit 5
How to use JQuery in js.erb of Rails6
[Rails] How to use Gem'rails-i18n' for Japanese support
How to use Dozer.mapper
How to use Gradle
[Ruby on Rails] How to use session method
[Rails] How to use PostgreSQL in Vagrant environment
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[rails] How to use devise helper method before_action: authenticate_user!
[Rails] I don't know how to use the model ...
[Java] How to use Map
[rails] How to post images
How to use Chain API
[Java] How to use Map
How to use Priority Queuing