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?
4 Answers
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.

- 279,434
- 135
- 377
- 622
-
2admin/blog works slightly better for me, but it's only a little and quite possibly totally subjective. – ere Mar 21 '12 at 17:06
-
11in Rails >3 `rails g scaffold admin/blog` – Tolik Kukul Jun 20 '13 at 04:47
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

- 4,092
- 1
- 35
- 28
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.

- 9,659
- 3
- 46
- 49
-
-
I think this is the best available at this point (Rails 7), though it still gets some route helpers wrong. – tgf Aug 07 '23 at 09:59
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.

- 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