Uninitialized constant error pertaining to Rails' namespaces

南楼画角 提交于 2020-01-06 20:08:09

问题


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

Error:

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

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

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

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

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

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

Thanks you!


回答1:


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

Works because your controller is

class FirefightersController
end

These

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

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

class Firefighters::FirefightersController

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.



来源:https://stackoverflow.com/questions/35462137/uninitialized-constant-error-pertaining-to-rails-namespaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!