嵌套命名空间和缺少模板
问题描述:
我想实现一个管理面板,我认为这将是很好的链接到屏幕左侧的不同标签,并使用AJAX在屏幕右侧显示这些标签。嵌套命名空间和缺少模板
我决定:admin
命名空间去我的模型资源的范围内(因为我需要:id
),所以我在router.rb做:
resources :my_model do
namespace :admin do
get "panel", to: "panel#index" #The route to display the main admin panel view
# for now let just assume I have one tab:
namespace :info_tab do
get "index", to: "info_tab#index"
end
end
end
在我的控制器中的文件
然后,我有这样的一个层次:
controllers/admin/info_tab/info_tab_controller.rb
在这个文件中我写的(这我不知道如果我这样做是正确的)
class Admin::InfoTab::InfoTabController < ApplicationController
def index
logger.debug("Index from info_tab_controller loaded!")
respond_to do |format|
format.js
end
end
end
我实现了remote: true
连结此选项卡后,我点击它来自控制器的方法被执行,但在我的服务器日志中我得到这个:
Started GET "/my_model/1/admin/info_tab/index" for ::1 at 2016-11-10 22:33:55 +0100
Processing by Admin::InfoTab::InfoTabController#index as JS
No template found for Admin::InfoTab::InfoTabController#index, rendering head :no_content
Completed 204 No Content in 69ms (ActiveRecord: 0.4ms)
为InfoTabController
我的索引视图根据地方:
views/admin/info_tab/index.js.erb
我做了什么错?
这种方法我试图做对吗?还是有更好的方法来构建它? (特别是路由)
答
Rails的查找视图的路径基于类似
/app/views/{namespace1}/{namespace2}/{controller}/{action}
,所以我会假设你需要的视图路径是
app/views/admin/info_tab/info_tab/index.*
除非您预计有许多不同的InfoTab
控制器(例如几十个),否则我还会建议您移动InfoTab
命名空间,所以你的控制器是: Admin::InfoTabController < ApplicationController
在这种情况下,你当前的视图路径应该工作。
如果你想了解更多关于轨道如何查看视图路径的信息,我会检查一个博客帖子,如https://climber2002.github.io/blog/2015/04/06/digging-rails-how-rails-finds-your-templates-part-4/