如何避免 Rails 脚手架将模型放入命名空间
Rails 3 脚手架生成器将模型类放置在命名空间内。 示例:
rails generate scaffold admin/portfolio
但我只想将控制器和视图放置在管理命名空间内。
我怎样才能避免这种情况?
Rails 3 scaffold generator places model classes inside namespace.
Example:
rails generate scaffold admin/portfolio
But I want only controllers and views to be placed inside admin namespace.
How can I avoid that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Rails 4 生成器有点不同。如果您使用scaffold_controller生成器,它将预先构建所有视图文件,但默认情况下它将查找名为Admin::Portfolio的模型。要加载正确的模型,只需将模型名称作为参数传递给生成器。
这将为您提供一个命名空间控制器和引用非命名空间模型的视图。
Rails 4 generators are a bit different. If you use the scaffold_controller generator it will pre-build all the view files, but by default it will look for a model called Admin::Portfolio. To load the correct model just pass the model name as an argument to the generator.
This will give you a namespaced controller and views that reference the non-namespaced model.
rails 生成模型组合
rails 生成控制器 Admin::Portfolio
rails generate model Portfolio
rails generate controller Admin::Portfolios
@RubyDev 建议 Ryan Bate 的 Nifty Generators 是正确的,但我不知道他为什么说使用 --skip - 型号选项。
Nifty Generators 实际上会完全执行您所要求的操作。只需将其添加到您的 Gemfile:
并运行:
这将在“admin”命名空间中创建普通支架的所有内容,其中包含控制器和视图,但模型不在命名空间中。
@RubyDev was right to suggest Ryan Bate's Nifty Generators, but I don't know why he said to use the --skip-model option.
Nifty Generators will actually do exactly what you are asking for. Simply add it to your Gemfile:
and run:
This will create everything a normal scaffold would with the controllers and views in an 'admin' namespace, but the model not in namespace.
按照 @tybro0103 更新
使用 nifty:generators: https://github.com/ryanb/nifty-generators
如果您已经生成了没有命名空间的模型或脚手架,并且想为管理命名空间再次生成,则可以跳过模型:
如果您希望脚手架生成包含所有字段的视图,请再次输入字段名称,例如:
我通常将两者一起执行,因此只需转到上一个命令并编辑它以添加 Admin:: & 就很容易了。 --跳过模型。
Updated as per @tybro0103
Use nifty:generators: https://github.com/ryanb/nifty-generators
If you have already generated the model or scaffold without namespace and would like to do it again for admin namespace, you can skip model:
If you would like the scaffold to generate views with all fields, please put the field names again, e.g:
I usually do the two together so its easy to just go to previous command and edit it to add Admin:: & --skip-model.
您可以相当简单地创建自己的生成器并用它们做任何您想做的事情:
在 Rails 4 中:
并且
将删除模型命名空间。
请记住,如果您自己生成scaffold_controller,则需要显式调用自定义生成器:
rails g my_controller 'account/users'
不幸的是,这仅处理控制器。我仍在寻找查看解决方案。
You can fairly simply create your own generators and do whatever you want with them:
In Rails 4:
and
Will remove the model namespacing.
Bear in mind if you are generating a scaffold_controller on its own you'll need to explicitly call your custom generator:
rails g my_controller 'account/users'
Unfortunately this only handles the controller. I'm still searching for a view solution.
您现在可以使用以下命令在 Rails 上(或至少在 5.1 上)执行此操作:
通过指定 --model-name Rails 不会自动尝试猜测模型名称。
You can do this now on Rails (or at least on 5.1) using the following command:
By specifying --model-name Rails does not automatically tries to guess the model name.
不久前,Nifty 生成器是正确的事情,但现在随着 Rails 4 的发布,它已经过时了。如果您想使用非命名空间模型生成管理脚手架,可以使用 rails-admin-scaffold 宝石。
Nifty generators was the right thing some time ago, but now as Rails 4 released it became outdated. If you want to generate admin scaffolding with non-namespaced model you can use rails-admin-scaffold gem.
最好的解决方案
所以它不会在表中生成前缀
The best solution
So it does not generate a prefix in the tables