使用铁路管理路线
我希望能够更好地理解 Rails 路线文件,但我自己无法弄清楚,因为它太复杂了。
基本上我有 3 个控制器。它们是:Admin、ManageProduct 和 ManageProductCategory(我有 2 个模型:Product、ProductCategory 和模型 ProductCategory has_many/belongs_to products 关系)
管理控制器操作:
- 索引(重定向到登录)
- 登录
- 注销
- 尝试
ManageProduct 控制器操作:
- 索引
- CRUD(删除、编辑、显示,列表)模型产品
ManageProductCategory
- 模型产品类别的索引
- CRUD(删除,编辑,显示,列表)
我希望能够管理我的应用程序路由,以便如果我在浏览器中键入:
mywebsite/admin
mywebsite/admin/login
mywebsite/admin/logout
mywebsite/admin/manage_product
mywebsite/admin/manage_product_category/1
mywebsite/admin/manage_product/delete
mywebsite/admin/manage_product/10
等等...
问题是我不知道如何设置我的路线文件,以便 Rails 理解 admin/manage_product 不是管理控制器操作...
注意:一切都已经正常工作(2 个模型的 CRUD 以及通过标准不推荐路线的操作链接
match ':controller(/:action(/:id(.:format)))'
真的感谢您的帮助和关注
问候
I want to be able to better understand rails routes file, but I can't figure it out by myself since it's too complex.
Basically I have 3 controllers. They are: Admin, ManageProduct and ManageProductCategory (I have 2 models: Product, ProductCategory and model ProductCategory has_many/belongs_to products relationship)
Admin controller actions:
- index (redirects to login)
- login
- logout
- attempt
ManageProduct controller actions:
- index
- CRUD (delete, edit, show, list) for model product
ManageProductCategory
- index
- CRUD (delete, edit, show, list) for model product_category
I want to be able to manage my application routes so that if I type in the browser:
mywebsite/admin
mywebsite/admin/login
mywebsite/admin/logout
mywebsite/admin/manage_product
mywebsite/admin/manage_product_category/1
mywebsite/admin/manage_product/delete
mywebsite/admin/manage_product/10
And so on...
The problem is I can't figure out how to setup my route files so that rails understand that admin/manage_product is not a admin controller action...
NOTICE: Everything is already working (CRUD for 2 models and links to action via standard not recommended route
match ':controller(/:action(/:id(.:format)))'
Really appreciate your help and attention
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是一个命名空间
这将为您提供以下 URL 帮助器方法:
要在 admin 命名空间下生成控制器,您需要在控制台下执行以下操作:
这将为您生成
admin
目录app/controllers
然后是products.rb
文件:现在,您可以使用 Devise 设置 admin 命名空间下的登录,Devise 是一个用于身份验证的 gem。您可以在这里进一步了解: https://github.com/plataformatec/devise/wiki/_pages< /a>
What you need is a namespace
This will give you the following URL helper methods:
To generate a controller under the admin namespace you need to do the following under your console:
This will generate for you the
admin
directory underapp/controllers
and then theproducts.rb
file:Now about, the login under the admin namespace you can set it up with Devise, which is a gem for authentication. You can go further in here: https://github.com/plataformatec/devise/wiki/_pages