Skip to content

Usage

Generate Resource

bash
$ rails g rest_api_generator:resource table_name attributes

This command will create:

  • Model and Migration: Using rails default model generator
  • Controller: A controller that implementes CRUD by inheritance of RestApiGenerator::ResourceController, or you can use eject option for create a controller that implements index, show, create, update and destroy methods.
  • Specs for the created controller
  • Factory bot factory for created model
  • Routes: with rails resources

Example

bash
$ rails g rest_api_generator:resource car name:string color:string

Will generate following controller and the other files:

ruby
# app/controllers/cars_controller.rb
class CarsController < RestApiGenerator::ResourceController
end

For a better experience you can override some methods from the default controller

Options

OptionGoalDefaultUsage Example
fatherGenerate nested resourcenil--father Users
scopeScope the resource for other route or namespace organizationnil--scope Api::V1
ejectEject the controller to high customizationfalsetrue
specChoose the spec format. Current options: "rspec" or "rswag"rspec--spec rswag