我在访问/users/newRails 4 时遇到错误Unable to autoload constant UsersController, expecting /app/controllers/users_controller.rb to define it.
这是控制器代码
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
Run Code Online (Sandbox Code Playgroud)
我的观点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 %>
Run Code Online (Sandbox Code Playgroud)
User 模型:
class User < ActiveRecord::Base
has_many :projects
has_many :pledges
has_many :posts
has_many :comments
end
Run Code Online (Sandbox Code Playgroud)
sea*_*cal 22
Rails期望控制器名称是多元化的.您发布的第一个文件内容的第一行写为单数:
在/app/controllers/users_controller.rb你有:
class User Controller <ApplicationController
相反,它应该是:
class Users Controller <ApplicationController
此Rails指南提供了在路由文件中定义资源的示例.此外,还有一个信息性说明解释了使用该resource方法定义路径将始终映射到控制器的复数名称.
这是指南中的信息性说明.
因为您可能希望对单个路径(/ account)和复数路径(/ accounts/45)使用相同的控制器,所以单个资源映射到多个控制器.因此,例如,资源:照片和资源:照片创建映射到同一控制器(PhotosController)的单个和多个路线.
source:资源路由:Rails默认
| 归档时间: |
|
| 查看次数: |
24562 次 |
| 最近记录: |