生成脚手架可以将控制器放在命名空间中吗?
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我第一次做的时候,我运行
然后重构控制器、视图等。目前我首选的解决方案是:
然后重构模型、单元测试和迁移;这是更少的工作。
如果有更好的答案,我会接受。
The first time I've done it, I run
and then refactored the controller, views, etc. My prefered solution at the moment is:
and then refactor the model, unit test and migration; it's less work.
If there's a better answer, I'll accept it.
您可以对 Rails < 执行此操作3:
或
对于导轨> 3:
或
You can do this for rails < 3:
or
For rails > 3:
or
对于 Rails 6:
假设您有一个如下所示的模型:
那么您可以使用以下方法执行此操作
(指定模型名称会阻止控制器引用默认模型 Admin::Foo)
注意:这并不完美;您必须修复视图和测试中的一堆路径错误 - 但它可以让您完成 80% 的工作。
For Rails 6:
assuming you have a model like:
then you can do this with
(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.
这个问题在 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.