Is there a way to generate a rails scaffold without the views, there has to be a better way then generating the scaffold and deleting the views and the view specs.
Asked
Active
Viewed 1.5k times
3 Answers
45
You can use rails g resource Foo bar:text

drexin
- 24,225
- 4
- 67
- 81
-
Note it will also do fixtures, yaml and routes. Fixtures and tests will depend on your testing framework. You can also do `rails g model...` and rails generate controller... for more restrictive output. – junky Mar 12 '12 at 19:36
-
6`rails g resource` is what I was looking for. generating the model and then a controller does not create a RESTful controller. – Ibrahim Muhammad Mar 12 '12 at 19:42
-
4`rails g resource` does not generate restful actions inside the controllers. – vishB Nov 18 '15 at 06:29
-
1@whistler See [my answer](http://stackoverflow.com/a/37597087/616644) for how to get the RESTful actions auto generated as well. – Rick Smith Jun 02 '16 at 16:15
18
If you would like to have the controllers generated in the normal fashion, try this:
rails g resource Foo bar:text
rails g scaffold_controller Foo --skip-template-engine
The first command generates the model and the second one uses the generated model to create the controller which includes the RESTful actions.
--skip-template-engine
causes the views to be omitted.

Rick Smith
- 9,031
- 15
- 81
- 85
-
1This won't work right away because the second command will error out (since you already defined the resource in the first and can't/shouldn't overwrite it just like that). See for a better answer: https://stackoverflow.com/a/39649519/5925094 – Rich Steinmetz Jun 05 '21 at 16:18
8
Not sure why these answers create the resource first when you can just generate the entire scaffold without the views but still get your controller methods and model.
rails g scaffold Foo bar:string --skip-template-engine

Juan Pablo Ugas
- 1,085
- 9
- 22