生成一个与模型名称不同的脚手架

发布于 2024-12-17 14:13:08 字数 695 浏览 0 评论 0 原文

是否可以为具有不同名称且不直接与我的模型名称关联的现有模型生成和搭建支架?

假设我有这个模型 Post 并且我已经有一个名为 posts_controller 的控制器,但我已经在使用了一堆方法,但只是时间问题我需要生成一个脚手架来通过模型 Post 填充我的表格帖子,但如果我尝试

rails gscaffold Post field:type.... --skip--migration

生成器仍然会创建一个名为posts_controller 可以指定它使用/生成的控制器名称:my_awesome_scaffold_controller

谢谢。

=======更新========

我想要在my_awesome_scaffold_controller中拥有的是我的模型帖子index的完整支架,新建编辑显示创建更新删除对于 Post 模型,不使用post_controller with 已经用于其他人完成的其他事情,我不想弄乱那里的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

这个俗人 2024-12-24 14:13:08

好吧...我不相信你所做的比仅仅将现有的 posts_controller 移到其他地方(例如图书馆或其他地方)更好,但要忠实于你实际要求的内容...

一个选项(如果您不介意将操作添加回 posts_controller)

  1. 将“post_controller.rb”复制到其他地方,
  2. 按照正常方式使用“post”运行脚手架(它将被命名为 posts_cotroller.rb)
  3. 将“posts-controller.rb”重命名为“my_awesome_posts_controller.rb”
  4. 复制回 posts_controller.rb
  5. 从“my-awesome”中提取所有内容并将其粘贴到现有 posts_controller.rb 的顶部/底部,

显然,如果 posts_controller 已经具有任何 RESTful 操作,则这将不起作用 - 也这意味着您和其他人正在处理同一个文件(尽管可能是不同的区域)。

或者,您可以为脚手架命名空间。类似于 admin/posts_controller.rb (或者在您的情况下:my_awesome/posts_controller.rb

这里有一个关于此的旧教程:http://icebergist.com/posts/restful-admin-namespaced-controller-using-scaffolding
无法保证它对您有多好/相关。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文