Rails scaffolding for JSON only

Viewed 3813

When I use rails g scaffold Model key:string value:string command it creates both controller and views (erb, scss, js). How can I generate only controller which will respond with only JSON format.

3 Answers

Use API flag in Rails 5+

Without having to modify configuration:

rails g scaffold TestLink --api

Pro tip: Use the --pretend flag to see what will be created before running:

rails g scaffold TestLink --api --pretend

If you already have a project setup as a normal rails app, you can make the generators api only by setting it up in config/application.rb.

config.generators do |g|
  g.api_only = true
end
Related