REST API with Ruby on Rails and ReactJS
I think this is an interesting idea, although I haven't personally used it when building REST APIs.

Here's some code which shows how it could be done.

I tested the following code to ensure that it works. To test it:


make a todo model with a :name column
add resources :todos to the routes
visit localhost:3000/todos


...

# app/controllers/todos_controller.rb
class TodosController &lt; ApplicationController
  include GenericController
  RecordClass = Todo
  def record_params
    params.require(:todo).permit(:name)
  end
end


You can see that there are only two points of customization in each controller.
You'll have to specify the RecordClass and your secure parameters method.

The following "generic controller" was built using Rails' default scaffold controller.
You can customize it to follow your own default controller logic.

# app/controllers/generic_controller.rb
module GenericController

  before_action :set_instance, only: [:show, :edit, :update, :destroy]

  def index
    @records = RecordClass.all
  end

  def show
  end

  def new
    @record = RecordClass.new
  end

  def edit
  end

  def create
    @record = RecordClass.new(record_params)
    respond_to do |format|
      if @record.save
        format.html { redirect_to @record, notice: '#{RecordClass} was successfully created.' }
        format.json { render :show, status: :created, location: @record }
      else
        format.html { render :new }
        format.json { render json: @record.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @record.update(record_params)
        format.html { redirect_to @record, notice: '#{RecordC;ass} was successfully updated.' }
        format.json { render :show, status: :ok, location: @record }
      else
        format.html { render :edit }
        format.json { render json: @record.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @record.destroy
    respond_to do |format|
      format.html { redirect_to records_url, notice: #{'RecordClass} was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    def set_record
      @record = RecordClass.find(params[:id])
    end