gpt4 book ai didi

ruby-on-rails - 将 Controller 路由到命名空间 :admin to/admin

转载 作者:行者123 更新时间:2023-12-04 03:45:09 25 4
gpt4 key购买 nike

我觉得这可能是一个愚蠢的问题,但已经晚了,我的头有点融化了..所以我很感激你的帮助。

我正在尝试映射网址 http://localhost:3000/admin到仪表板 Controller ,但我非常失败。也许这甚至不可能或完全错误的想法,但无论如何我的路线看起来像这样,是的

namespace :admin do
resources :dashboard, { :only => [:index], :path => '' }
...
end

和我简单的dashboard_controller.rb
class Admin::DashboardController < ApplicationController
before_filter :authenticate_user!
filter_access_to :all

def index
@schools = School.all
end
end

我的 View 位于 views/admin/dashboard/index.html.erb

感谢您提供任何意见

最佳答案

如果您要做的只是路由 /admin到那个仪表板 Controller ,那么你通过像这样命名空间来过度复杂化它。

使用这样的嵌套资源命名空间意味着它是 /admin/dashboards:index采取行动而不是清洁 /admin路线(您可以通过在命令行运行 rake routes 来获取路线列表来验证)。

选项 1:您打算将其命名为

# putting this matched route above the namespace will cause Rails to 
# match it first since routes higher up in the routes.rb file are matched first
match :admin, :to => 'admin/dashboards#index'
namespace :admin do
# put the rest of your namespaced resources here
...
end

选项 2:你不是想像那样命名它

路线:
match :admin, :to => 'dashboards#index'

Controller :
# Remove the namespace from the controller
class DashboardController < ApplicationController
...
end

View 应移回:
views/dashboards/index.html.erb

更多信息: http://guides.rubyonrails.org/routing.html

关于ruby-on-rails - 将 Controller 路由到命名空间 :admin to/admin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9030086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com