路由错误未初始化常量 UsersController

Routing Error uninitialized constant UsersController

我正在编写学习 Rails 开发的教程。

如果这个问题是多余的,我深表歉意。可能是我的初学者经验水平导致很难找到相关的现有答案。

非常感谢任何帮助。

本课的目的是建立一个类似 Reddit 的网站。在本课中,我应该使用 CarrierWave 和 MiniMagick gem 使用 S3 存储实现图像上传。据我所知,我已严格按照课程说明进行操作,但也许我遗漏了什么。

当我尝试从编辑视图上传图像文件时,发生了这种情况。

这里是错误:

app/controllers/users_controller.rb:5:in `update'


Rendered /Users/gregorybowler/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (8.5ms)
Rendered /Users/gregorybowler/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.5ms)
Rendered /Users/gregorybowler/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.4ms)
Rendered /Users/gregorybowler/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (64.7ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x007fd34a40cb10 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007fd34a40ce30 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007fd3493e8d88 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]


Started PATCH "/users/8" for ::1 at 2015-07-03 22:05:20 -0700

ActionController::RoutingError (uninitialized constant UsersController):
activesupport (4.2.1) lib/active_support/inflector/methods.rb:261:in `const_get'
activesupport (4.2.1) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.1) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.1) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.1) lib/active_support/inflector/methods.rb:259:in `constantize'

这是我的 user_controller.rb 文件:

class UsersController < ApplicationController
  before_action :authenticate_user!

def update
  if current_user.update_attributes(user_params)
    flash[:notice] = "User information updated"
    redirect_to edit_user_registration_path
  else
    flash[:error] = "Invalid user information"
    redirect_to edit_user_registration_path
  end
end

 private

 def user_params
   params.require(:user).permit(:name, :avatar)
 end
end

这里是config/routes.rb

Rails.application.routes.draw do
  devise_for :users
  resources :users, only: [:update]

resources :topics do
  resources :posts, except: [:index]
end

  get 'about' => 'welcome#about'

  root to: 'welcome#index'
end

当我看到它时,我皱了整整 10 分钟。该错误表明它没有找到您的 UsersController 常量,即您的 class.

你说它在 user_controller.rb。但是 Rails 自动加载只会期望找到 UsersController,请注意 's',您猜对了,app/controllers/users_controller.rb。 (很高兴记住惯例规定 #{thing}Controller 是复数)