Skip to content

Instantly share code, notes, and snippets.

@rahulnyk
rahulnyk / crud.rb
Created August 10, 2020 15:05
A Ruby on Rails controller concern which adds basic CRUD operations for any model.
# frozen_string_literal: true
# Controller concern for basic crud.
# Can be added to any controller like so
# include Crud
# Assumptions:
# 1. Model class defines a constant called WHITELIST_FIELDS
# as an array of symbols for attributed that are allowed for creating a record.
# 2. Model is serialized elsewhwere. This can be done either by overwriting as_json
# method in the model class, or by using separate serializers. By default as_json