Skip to content

Instantly share code, notes, and snippets.

@lucaswinningham
Created February 6, 2019 16:47
Show Gist options
  • Save lucaswinningham/4b9e04b313af62d6dbab63bed8edd78c to your computer and use it in GitHub Desktop.
Save lucaswinningham/4b9e04b313af62d6dbab63bed8edd78c to your computer and use it in GitHub Desktop.
Rails Generators
$ rails g scaffold_controller foo
app/controllers/foos_controller.rb
class FoosController < ApplicationController
  before_action :set_foo, only: [:show, :update, :destroy]

  # GET /foos
  def index
    @foos = Foo.all

    render json: @foos
  end

  # GET /foos/1
  def show
    render json: @foo
  end

  # POST /foos
  def create
    @foo = Foo.new(foo_params)

    if @foo.save
      render json: @foo, status: :created, location: @foo
    else
      render json: @foo.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /foos/1
  def update
    if @foo.update(foo_params)
      render json: @foo
    else
      render json: @foo.errors, status: :unprocessable_entity
    end
  end

  # DELETE /foos/1
  def destroy
    @foo.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_foo
      @foo = Foo.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def foo_params
      params.fetch(:foo, {})
    end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment