生成脚手架可以将控制器放在命名空间中吗?

发布于 2024-09-06 01:12:43 字数 58 浏览 3 评论 0原文

我想在 Rails 应用程序中生成脚手架,像往常一样生成模型,但将控制器放在管理命名空间内。是否可以?

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?

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

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

发布评论

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

评论(4

欲拥i 2024-09-13 01:12:43

我第一次做的时候,我运行

script/generate scaffold blog

然后重构控制器、视图等。目前我首选的解决方案是:

script/generate scaffold admin::blog

然后重构模型、单元测试和迁移;这是更少的工作。

如果有更好的答案,我会接受。

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.

客…行舟 2024-09-13 01:12:43

您可以对 Rails < 执行此操作3:

script/generate scaffold Blog title:string

script/generate scaffold admin::blog title:string

对于导轨> 3:

rails g scaffold Blog title:string

rails g scaffold admin/blog title:string

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
单调的奢华 2024-09-13 01:12:43

对于 Rails 6:

假设您有一个如下所示的模型:

rails g model Foo name:string

那么您可以使用以下方法执行此操作

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

(指定模型名称会阻止控制器引用默认模型 Admin::Foo)

注意:这并不完美;您必须修复视图和测试中的一堆路径错误 - 但它可以让您完成 80% 的工作。

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.

贪恋 2024-09-13 01:12:43

这个问题在 stackoverflow 上被广泛询问。我也遇到了这个问题,但没有找到标准的解决方案。

因此,我创建了 rails-admin-scaffold gem (目前仅限 Rails 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.

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