Pablo Fernandez
Pablo Fernandez

Reputation: 287410

Can generate scaffold put the controller in a namespace?

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?

Upvotes: 27

Views: 16451

Answers (4)

Confused Vorlon
Confused Vorlon

Reputation: 10446

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.

Upvotes: 8

ilgam
ilgam

Reputation: 4420

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

Upvotes: 8

dhampik
dhampik

Reputation: 144

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.

Upvotes: 4

Pablo Fernandez
Pablo Fernandez

Reputation: 287410

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.

Upvotes: 21

Related Questions