【问题标题】:Rails - using namespace controllers to organize filesRails - 使用命名空间控制器来组织文件
【发布时间】:2017-02-27 03:54:56
【问题描述】:

我正在尝试学习命名空间。

我之前就这个话题问过几个问题,但我不明白发生了什么。

我在控制器的文件夹中创建了一个名为“功能”的文件夹。在其中,我保存了一个名为 app_roles_controller.rb 的文件。

该控制器的第一行是:

class Features::AppRolesController < ApplicationController

功能文件夹的目的是让我可以更好地组织我的文件(就是这样)。

在我的 routes.rb 中,我尝试过:

resources :app_roles, :namespace => "features", :controller => "app_roles"

我也试过了:

namespace :features do
  resources :app_roles 
end

我有一个名为 app_role.rb 的模型(顶级),我有一个保存为 views/features/app_roles 的视图文件夹,其中包含索引、显示等文件。我的架构中的表称为“app_roles”。

当我为 app_roles 搜索路由时,我得到:

Paths Containing (app_role):
app_roles_path  GET /app_roles(.:format)    
app_roles#index {:namespace=>"features"}

POST    /app_roles(.:format)    
app_roles#create {:namespace=>"features"}

new_app_role_path   GET /app_roles/new(.:format)    
app_roles#new {:namespace=>"features"}

edit_app_role_path  GET /app_roles/:id/edit(.:format)   
app_roles#edit {:namespace=>"features"}

app_role_path   GET /app_roles/:id(.:format)    
app_roles#show {:namespace=>"features"}

PATCH   /app_roles/:id(.:format)    
app_roles#update {:namespace=>"features"}

PUT /app_roles/:id(.:format)    
app_roles#update {:namespace=>"features"}

DELETE  /app_roles/:id(.:format)    
app_roles#destroy {:namespace=>"features"}

我不明白我做错了什么。

当我尝试时:

http://localhost:3000/app_roles#index

我收到一条错误消息:

uninitialized constant AppRolesController

当我尝试时:

http://localhost:3000/features/app_roles#index

我收到一条错误消息:

No route matches [GET] "/features/app_roles"

我正在寻找有关如何设置的简单英语解释。我已经尝试过编程红宝石书(几次)。

请您帮我了解在我的 rails 应用程序中引入组织文件需要发生什么?

【问题讨论】:

  • 你真的触发了这个网址吗? `localhost:3000/features/app_roles#index? Remove the #index` 在您的网址中。
  • @araratan - 这也不起作用。当我尝试:localhost:3000/features/app_roles 时,我收到一条错误消息:No route matches [GET] "/features/app_roles"
  • 您是否阅读过有关布线的 Rails 指南?它通常是一个值得一看的好地方。关于命名空间的部分在这里:guides.rubyonrails.org/… 它建议您尝试类似:resources :app_roles, module: 'features' 这将导致索引页面的路径类似于features_app_roles_path(但我建议阅读该指南以了解更多关于它的工作原理和期望它做什么)
  • 是的 - 我已经读过了。我找不到任何没有行话的东西。我对这些资源感到很困惑。无论如何,谢谢 - 我会继续努力。理解这一点可能离我太远了。也许将所有内容都保持在顶层杂乱无章,这比弄清楚如何将编码指南翻译成英文更容易。

标签: ruby-on-rails ruby routes namespaces


【解决方案1】:

看起来您的其他尝试实际上是正确的。 As outlined in the Rails documentation,如果您想在命名空间features 下为资源app_roles 生成路由,可以将以下内容添加到您的routes.rb 文件中:

namespace :features do
  resources :app_roles
end

现在,您运行rake routes,您将看到以下内容:

                Prefix Verb   URI Pattern                            Controller#Action
    features_app_roles GET    /features/app_roles(.:format)          features/app_roles#index
                       POST   /features/app_roles(.:format)          features/app_roles#create
 new_features_app_role GET    /features/app_roles/new(.:format)      features/app_roles#new
edit_features_app_role GET    /features/app_roles/:id/edit(.:format) features/app_roles#edit
     features_app_role GET    /features/app_roles/:id(.:format)      features/app_roles#show
                       PATCH  /features/app_roles/:id(.:format)      features/app_roles#update
                       PUT    /features/app_roles/:id(.:format)      features/app_roles#update
                       DELETE /features/app_roles/:id(.:format)      features/app_roles#destroy

例如,第一行表示如果您向 /features/app_roles 发出 GET 请求,它将被路由到 features/app_roles 控制器中的 index 操作。

换句话说,如果您访问http://localhost:3000/features/app_roles,它会将请求路由到位于app/controllers/features/app_roles_controller.rb 中的index 操作,它期望具有Features::AppRolesController 类。

所以你的app/controllers/features/app_roles_controller.rb 文件应该是这样的:

class Features::AppRolesController < ApplicationController
  def index
  end
end

【讨论】:

  • 这是否意味着我还创建了一个名为“Features”的模型文件夹并将 app_role.rb 保存在该文件夹中?如果是这样,我是否需要做一些事情来传达架构中表的名称是“app_roles”而不是包含对“功能”的引用。
猜你喜欢
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2023-03-11
  • 2012-07-08
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
相关资源
最近更新 更多