简体   繁体   English

如何在 Rails 中的命名空间内生成 controller

[英]How to generate controller inside namespace in rails

I have namespace admin in controller, and I want to generate a controller inside of the admin folder.我在 controller 中有命名空间admin ,我想在admin文件夹中生成一个 controller。 How can i do it with a Rails command?我如何使用 Rails 命令执行此操作?

Try rails g controller admin/users if you want a users controller inside of the admin namespace.如果您希望在admin命名空间内有一个users控制器,请尝试使用rails g controller admin/users Of course, exchange users with whatever controller name that you'd like.当然,用您喜欢的任何控制器名称交换users

使用这个命令..

rails generate controller namespace_name/controller_name

If you want to gen it:如果你想生成它:

rails generate controller namespace_name/controller_name

And If you want to rollback this step using:如果您想使用以下方法回滚此步骤:

rails destroy controller namespace_name/controller_name

In your case it will be在你的情况下,它将是

$ rails g controller admin/controller_name

If we have an app structure like如果我们有一个类似的应用程序结构

  • ---controllers ---控制器
  • --api --api
  • -v1 -v1

and want to generate controller, do:并想要生成控制器,请执行以下操作:

$ rails g controller api/v1/controller_name

In general一般来说

$ rails g controller namespace_1/namespace_2/...../controller_name

You can also use the scope resolution operator to generate controllers within a namespace您还可以使用 scope 解析运算符在命名空间内生成控制器

rails g controller Admin::YourControllerName

or to generate controllers within nested namespaces或者在嵌套命名空间中生成控制器

rails g controller Admin::YourNestedNamespace::YourControllerName

Ref: What is Ruby's double-colon `::`? Ref: 什么是 Ruby 的双冒号`::`? , What does:: (double colon) mean in Ruby? , ::(双冒号)在 Ruby 中是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM