24

I want to generate the scaffold in a Rails app, generating the model as usual but having the controller inside the admin namespace. Is it possible?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

4 Answers4

20

The first time I've done it, I run

script/generate scaffold blog

and then refactored the controller, views, etc. My prefered solution at the moment is:

script/generate scaffold admin::blog

and then refactor the model, unit test and migration; it's less work.

If there's a better answer, I'll accept it.

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622
8

You can do this for rails < 3:

script/generate scaffold Blog title:string

or

script/generate scaffold admin::blog title:string

For rails > 3:

rails g scaffold Blog title:string

or

rails g scaffold admin/blog title:string
ilgam
  • 4,092
  • 1
  • 35
  • 28
6

For Rails 6:

assuming you have a model like:

rails g model Foo name:string

then you can do this with

rails g scaffold_controller Admin/Foo name:string --model-name="Foo"

(specifying the model name stops the controller from referring to the model Admin::Foo which would be the default)

NB: this isn't perfect; You'll have to fix up a bunch of path errors in your views and tests - but it gets you 80% of the way there.

Confused Vorlon
  • 9,659
  • 3
  • 46
  • 49
4

This question is pretty widely asked on stackoverflow. And I also faced this problem and found no standard solution for that.

So, I created rails-admin-scaffold gem (for now it's Rails 4 only) which automates this process and wrote an article with more detailed explanation. Hope it would be helpful for someone.

dhampik
  • 144
  • 2
  • 4
  • @ArnoldRoa I also encountered such issue. If there's a model called Admin, then it is impossible to have `Admin` namespace -- because rails would namespace it with a module with same name -- therefore, it would be a name conflict. – songyy Jul 23 '15 at 14:27