Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails scaffolding for JSON only

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.

like image 875
kleverigheid Avatar asked Mar 16 '16 18:03

kleverigheid


People also ask

What is the difference between static and dynamic scaffolding?

Static Scaffolding needs user insertion commands to generate data fields, whereas Dynamic Scaffolding generates the entire content at run time.

What is a Ruby on Rails scaffold?

Rails scaffolding is a quick way to generate some of the major pieces of an application. If you want to create the models, views, and controllers for a new resource in a single operation, scaffolding is the tool for the job.


2 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
like image 96
Matt Avatar answered Nov 15 '22 08:11

Matt


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
like image 21
Cereal Avatar answered Nov 15 '22 10:11

Cereal