与Rails命名空间有关的未初始化常量错误 [英] Uninitialized constant error pertaining to Rails' namespaces

查看:80
本文介绍了与Rails命名空间有关的未初始化常量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾发布过此问题,但是更改了一些变量名并意识到我的错误为时已晚,因此该代码不会改变。

I posted this question before, but changed some variable names and realized my mistake too late, so this code is unaltered in any way.

错误:


路由错误未初始化的常量Firefighters Rails.root:

/ Users / Vladdy / Dropbox / dev / firestaff

Routing Error uninitialized constant Firefighters Rails.root:
/Users/Vladdy/Dropbox/dev/firestaff

这是来自 routes.rb 的相关错误位:

Here's the relevant, erroring bit from routes.rb:

  # get '/firefighters/dashboard' => 'firefighters#dashboard'
  namespace :firefighters do
    get '/dashboard'        => 'firefighters#dashboard' # Namely, this one.
    get '/dashboard/:date'  => 'firefighters#dashboard'
    get '/account'          => 'firefighters#account'
    get '/edit'             => 'firefighters#edit'
    get '/approve'          => 'firefighters#approve'
  end

如果我取消注释第1行,并发表评论 / dashboard ,仪表板可以通过 FirefightersController#dashboard 正常加载。

If I uncomment line 1, and comment get /dashboard, the dashboard loads just fine through FirefightersController#dashboard.

我很努力地检查了我所有的模型名称是否都是单数: Accountant Engineer Firefighter 和我所有的控制器都是复数形式: AccountingController EngineeringController ,等等。

I painstakingly checked that all of my model names are singular: Accountant, Engineer, Firefighter and all of my controllers are plural: AccountingController, EngineeringController, etc.

这是错误的堆栈跟踪: http://pastebin.com / NpvY2EYy

Here is the stack trace of the error: http://pastebin.com/NpvY2EYy

我希望这是足够的信息。再次-SOS!我不知道如何诊断这个问题。如果有关系,我将使用ruby 2.3.0和rails 4.2.5

I hope this is enough information. Once again - SOS! I do no know how to diagnose this problem. If it matters, I'm using ruby 2.3.0 and rails 4.2.5

谢谢!

推荐答案

get '/firefighters/dashboard' => 'firefighters#dashboard'

有效,因为您的控制器是

Works because your controller is

class FirefightersController
end

这些

namespace :firefighters do
  get '/dashboard'        => 'firefighters#dashboard' # Namely, this one.
  get '/dashboard/:date'  => 'firefighters#dashboard'
  get '/account'          => 'firefighters#account'
  get '/edit'             => 'firefighters#edit'
  get '/approve'          => 'firefighters#approve'
end

不起作用,因为Rails期望使用模块您的控制器应该是

Don't work because rails is expecting a module so your controller should be

class Firefighters::FirefightersController

如果只对URL而不是应用程序的文件夹结构感到困扰,可以这样做

You could if you're only bothered about the url, not about the folder structure for your app do

scope '/firefighters' do
  get '/dashboard'        => 'firefighters#dashboard' # Namely, this one.
  get '/dashboard/:date'  => 'firefighters#dashboard'
  get '/account'          => 'firefighters#account'
  get '/edit'             => 'firefighters#edit'
  get '/approve'          => 'firefighters#approve'
end

在这种情况下,您可以将控制器原样保留

In which case you could leave your controller as it is.

这篇关于与Rails命名空间有关的未初始化常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆