30

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.

Ibrahim Muhammad
  • 2,628
  • 3
  • 27
  • 37

3 Answers3

44

You can use rails g resource Foo bar:text

drexin
  • 23,521
  • 4
  • 61
  • 79
  • 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
16

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
  • 8,219
  • 13
  • 75
  • 81
5

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
  • 908
  • 8
  • 21