Mr_Nizzle
Mr_Nizzle

Reputation: 6714

Generating an Scaffold with a different name from the model

Is is posible to generate and scaffold for an existing model with a different name not associated directly to the name of my model?

Let's say I have this model Post and I already have a controller named posts_controller but I have a bunch of methods there that I'm already using, but as a matter of time I need to generate an scaffold to fill in my table posts trough the model Post but if I try

rails g scaffold Post field:type.... --skip--migration

The generator would still be looking to create a controller named posts_controller is is possible to specify the controller name It'd using/generating to something like: my_awesome_scaffold_controller ?

Thanks.

======= UPDATE ========

What I want to have in my_awesome_scaffold_controller is the complete scaffold for my model post index, new, edit, show, create, update, delete for the Post model without using the post_controller with is already in use for other stuff done by somebody else and I don't wanna mess up the code there.

Upvotes: 0

Views: 357

Answers (1)

Taryn East
Taryn East

Reputation: 27747

ok... I'm not convinced that what you're doing is better than just, say, moving the existing posts_controller away somewhere else (eg a library or whatever) but to stay true to what you actually asked...

One option (if you don't mind adding the actions back into posts_controller)

  1. copy "post_controller.rb" to somewhere else
  2. run scaffold as per normal with "post" (it will be named posts_cotroller.rb)
  3. rename "posts-controller.rb" to "my_awesome_posts_controller.rb"
  4. copy back posts_controller.rb
  5. pull everything out of "my-awesome" and paste it into the top/bottom of existing posts_controller.rb

obviously this won't work if the posts_controller already has any of the RESTful actions - also it means you and the other person are working on the same file (though likely different areas).

Alternatively you could name-space your scaffold. Similar to admin/posts_controller.rb (or in your case: my_awesome/posts_controller.rb)

There's an old tutorial on this here: http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding Can't vouch for how good/relevant it is for you.

Upvotes: 1

Related Questions