gpt4 book ai didi

ruby-on-rails - 出现无法自动加载常量 UsersController 的错误

转载 作者:行者123 更新时间:2023-12-03 21:30:17 25 4
gpt4 key购买 nike

我在访问 /users/new 时遇到错误在 Rails 4 中为 Unable to autoload constant UsersController, expecting /app/controllers/users_controller.rb to define it .

这是 Controller 代码

    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

以及 new.html.erb 的 View 我有:
  <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 %>
User模型:
class User < ActiveRecord::Base
has_many :projects
has_many :pledges
has_many :posts
has_many :comments
end

最佳答案

Rails 期望 Controller 名称为复数形式。您发布的第一个文件内容的第一行写为单数:

/app/controllers/users_controller.rb你有:

class UserController < ApplicationController



相反,它应该是:

class UsersController < ApplicationController



Rails Guide提供了在路由文件中定义资源的示例。此外,还有一个信息说明解释了如何使用 resource 定义路由。方法将始终映射到 Controller 的复数名称。

这是指南中的信息说明。

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).



来源: Resource Routing: The Rails Default

关于ruby-on-rails - 出现无法自动加载常量 UsersController 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19085743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com