UserController中的NoMethodError #new

时间:2015-03-03 20:56:32

标签: ruby-on-rails ruby-on-rails-3

我在我的应用程序中不断收到关于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'

1 个答案:

答案 0 :(得分:0)

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

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