使用铁路管理路线

发布于 2024-10-08 12:21:31 字数 968 浏览 0 评论 0原文

我希望能够更好地理解 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 技术交流群。

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

发布评论

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

评论(1

陌上青苔 2024-10-15 12:21:31

您需要的是一个命名空间

# Rails 2.3.x
map.namespace :admin do |admin|
  map.resources :products
end

# Rails 3
namespace "admin" do
  resources :products
end

这将为您提供以下 URL 帮助器方法:

admin_products_path    GET  { :controller => "admin/products", :action => "index" }
new_admin_product_path GET  { :controller => "admin/products", :action => "new" }
admin_products_path    POST { :controller => "admin/products", :action => "create" }

要在 admin 命名空间下生成控制器,您需要在控制台下执行以下操作:

$ rails generate controller admin/products

这将为您生成 admin 目录app/controllers 然后是 products.rb 文件:

class Admin::ProductsController < ApplicationController
end

现在,您可以使用 Devise 设置 admin 命名空间下的登录,Devise 是一个用于身份验证的 gem。您可以在这里进一步了解: https://github.com/plataformatec/devise/wiki/_pages< /a>

What you need is a namespace

# Rails 2.3.x
map.namespace :admin do |admin|
  map.resources :products
end

# Rails 3
namespace "admin" do
  resources :products
end

This will give you the following URL helper methods:

admin_products_path    GET  { :controller => "admin/products", :action => "index" }
new_admin_product_path GET  { :controller => "admin/products", :action => "new" }
admin_products_path    POST { :controller => "admin/products", :action => "create" }

To generate a controller under the admin namespace you need to do the following under your console:

$ rails generate controller admin/products

This will generate for you the admin directory under app/controllers and then the products.rb file:

class Admin::ProductsController < ApplicationController
end

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

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