Ruby-on-rails 出现无法自动加载常量 UsersController 的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19085743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting error as Unable to autoload constant UsersController
提问by Preethika
I am getting an error while visiting /users/newin Rails 4 as Unable to autoload constant UsersController, expecting /app/controllers/users_controller.rb to define it.
而来访的,我得到一个错误/users/new的轨道4作为Unable to autoload constant UsersController, expecting /app/controllers/users_controller.rb to define it。
Here is the controller code
这是控制器代码
class UserController < ApplicationController
def new
@user = User.new
end
def create
@user = User.new(params[:user]).permit(:email, :password,:password_confirmation)
respond_to do |format|
if @user.save
format.html { redirect_to new_user_path, notice: 'User was successfully created.' }
format.json { render action: 'show', status: :created, location: @user }
else
format.html { render action: 'new' }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
end
And the view for new.html.erbI have is:
我的观点new.html.erb是:
<h1>Sign up</h1>
<%= form_for(@user) do |f| %>
<% if @user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@user.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Create my account" %>
</div>
<% end %>
Usermodel:
User模型:
class User < ActiveRecord::Base
has_many :projects
has_many :pledges
has_many :posts
has_many :comments
end
回答by sealocal
Rails expects controller names to be pluralized. The very first line of the the first file contents you posted is written as singular:
Rails 期望控制器名称为复数形式。您发布的第一个文件内容的第一行写为单数:
In /app/controllers/users_controller.rbyou have:
在/app/controllers/users_controller.rb你有:
class UserController < ApplicationController
类用户控制器 < 应用控制器
Instead, it should be:
相反,它应该是:
class UsersController < ApplicationController
类用户控制器 < 应用控制器
This Rails Guideprovides an example for defining a resource in your routes file. Further, there is an informational note that explains that defining a routes with the resourcemethod will always map to the pluralized name of the controller.
本Rails 指南提供了一个在路由文件中定义资源的示例。此外,有一条信息说明解释了使用该resource方法定义路由将始终映射到控制器的复数名称。
This is the informational note from the guide.
这是指南中的信息说明。
Because you might want to use the same controller for a singular route (/account) and a plural route (/accounts/45), singular resources map to plural controllers. So that, for example, resource :photo and resources :photos creates both singular and plural routes that map to the same controller (PhotosController).
因为您可能希望对单数路由 (/account) 和复数路由 (/accounts/45) 使用相同的控制器,所以单一资源映射到多个控制器。因此,例如,资源 :photo 和资源 :photos 创建映射到同一控制器 (PhotosController) 的单数和复数路由。

