添加管理命名空间时出现路由错误

发布于 2024-12-07 08:52:47 字数 1040 浏览 0 评论 0原文

我向我的应用程序添加了一个 Admin 命名空间,因此当登录到管理区域时,它必须是这样的: admin/websites 和 admin/page/8

这就是我的 paths.rb 中的内容

namespace :admin do |admin|
  match '/' => 'dashboard#index'
  resources :websites
  resources :pages
  resources :sessions
  get 'login' => 'sessions#new', :as => 'login'
  get 'logout' => 'sessions#destroy', :as => 'logout'  
end

我有 admin_controller.rb在应用程序/控制器目录中。

class Admin::BaseController < ApplicationController
  protect_from_forgery
  include UrlHelper
  ...

我在 app/controllers 中创建了一个 admin 目录。所以我在 app/controllers/admin/websites_controller.rb 里面有这个

class Admin::WebsitesController < ApplicationController

其他一些答案建议类 Admin::WebsitesController < Admin::BaseController,但这对我来说从来没有用过。如果我错了请告诉我。

因此,在我的布局文件(app/views/layouts/application.html.erb)中,我有像这样的链接 edit_admin_website_path(@website) ,它给了我路由错误路由错误没有路由匹配{:action=>"编辑", :controller=>"管理/网站"} 为什么yyy?! :(

I added an Admin namespace to my app so when logging in to the administration area, it would have to be like this: admin/websites and admin/page/8

So this is what I have in my routes.rb

namespace :admin do |admin|
  match '/' => 'dashboard#index'
  resources :websites
  resources :pages
  resources :sessions
  get 'login' => 'sessions#new', :as => 'login'
  get 'logout' => 'sessions#destroy', :as => 'logout'  
end

I have admin_controller.rb in app/controllers directory.

class Admin::BaseController < ApplicationController
  protect_from_forgery
  include UrlHelper
  ...

I created an admin directory inside app/controllers. So I have this inside app/controllers/admin/websites_controller.rb

class Admin::WebsitesController < ApplicationController

Some other answers suggested class Admin::WebsitesController < Admin::BaseController, but that never worked for me. If I'm wrong please let me know.

So then in my layout file (app/views/layouts/application.html.erb) I have links like this one edit_admin_website_path(@website) that give me routing errors Routing Error No route matches {:action=>"edit", :controller=>"admin/websites"} Whyyyy?! :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

剑心龙吟 2024-12-14 08:52:47

在 admin 目录中添加一个名为 application_controller.rb 的文件,其中包含以下内容:

class Admin::ApplicationController < ApplicationController
end

然后,对于该目录中的每个控制器,扩展 Admin::ApplicationController 类。

你尝试过这个吗?

admin_edit_website_path(@website)

Add a file named application_controller.rb in the admin directory with this content:

class Admin::ApplicationController < ApplicationController
end

Then, for each controller on this directory, extend the Admin::ApplicationController class.

Did you try this?

admin_edit_website_path(@website)
三生路 2024-12-14 08:52:47

Rails 命名空间依赖于文件夹结构来加载正确的类。您应该这样构造它:

app/controllers
  admin_controller.rb # class AdminController < ApplicationController

app/controllers/admin
  websites_controller.rb # class Admin::WebsitesController < AdminController

AdminController 应该在 admin 文件夹之外定义。如果把它放在那里,你必须将其称为 Admin::AdminController ,这有点奇怪。事实上,为了清楚起见,您可以将其称为 AdminNamespaceController

您还可以使用railsgenerate,它会在预期的位置为您进行设置,尽管我不认为它会创建供您继承的命名空间基类。

Rails namespaces rely on folder structure for loading the right classes. You should structure it like this:

app/controllers
  admin_controller.rb # class AdminController < ApplicationController

app/controllers/admin
  websites_controller.rb # class Admin::WebsitesController < AdminController

The AdminController should be defined outside the admin folder. If put it in there you'd have to refer to it as Admin::AdminController which is a little odd. In fact, you could call it AdminNamespaceController to be clear.

You can also use rails generate which will set things up for you in the expected places, although I don't think it creates the namespace base class for you to inherit from.

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