UserController 中的 NoMethodError#new

NoMethodError in UserController#new

在 Michael Hartl Ruby Rails 教程中,我的应用程序从 7.2.1 开始不断出现以下错误。出于某种原因,它不接受注册表单中的 nil @user 字段。

undefined method `users_path' for #<#<Class:0x007ff87141aef8>:0x007ff870cf0d30>

new.html.erb

<% provide(:title, "Sign up") %>
<h1>Sign up</h1>

<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user) do |f| %>
      <%= f.label :name %>
      <%= f.text_field :name %>

      <%= f.label :email %>
      <%= f.email_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Create my account", class: "btn btn-primary" %>
    <% end %>
  </div>
</div>

User_controller.rb

class UserController < ApplicationController

  def new
    @user = User.new
  end

  def show
    @user = User.find(params[:id])
  end

end

Routes.rb

Rails.application.routes.draw do

  resources :user

  get 'user/new'

  root 'static_pages#home'

  get 'help' => 'static_pages#help'

  get 'about' => 'static_pages#about'

  get 'contact' => 'static_pages#contact'

  get 'signup' => 'user#new'

您需要将 resources :user 更改为 resources :users 并通过添加

在控制器中定义索引操作
def index
end