添加管理命名空间时出现路由错误
我向我的应用程序添加了一个 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 admin 目录中添加一个名为 application_controller.rb 的文件,其中包含以下内容:
然后,对于该目录中的每个控制器,扩展 Admin::ApplicationController 类。
你尝试过这个吗?
Add a file named application_controller.rb in the admin directory with this content:
Then, for each controller on this directory, extend the Admin::ApplicationController class.
Did you try this?
Rails 命名空间依赖于文件夹结构来加载正确的类。您应该这样构造它:
AdminController
应该在 admin 文件夹之外定义。如果把它放在那里,你必须将其称为Admin::AdminController
,这有点奇怪。事实上,为了清楚起见,您可以将其称为AdminNamespaceController
。您还可以使用
railsgenerate
,它会在预期的位置为您进行设置,尽管我不认为它会创建供您继承的命名空间基类。Rails namespaces rely on folder structure for loading the right classes. You should structure it like this:
The
AdminController
should be defined outside the admin folder. If put it in there you'd have to refer to it asAdmin::AdminController
which is a little odd. In fact, you could call itAdminNamespaceController
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.