Ruby on Rails - nil的未定义方法`errors':NilClass

时间:2016-10-22 12:10:12

标签: ruby-on-rails ruby

这个问题似乎重复,但我已经检查了所有其他相关问题,并且没有一个答案适用,因为我已经尝试了所有这些。

我正在尝试创建用户注册表单。

我的用户模型(user.rb)是这样的。

class User < ApplicationRecord
  attr_accessor :password
  EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i
  validates :username, :presence => true, :uniqueness => true, :length => { :in => 4..20 }
  validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
  validates :password, :confirmation => true #password_confirmation attr
  validates_length_of :password, :in => 6..20, :on => :create
end

用户控制器(users_controller.rb)就像这样

class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create

    @user = User.new(user_params)

    if @user.save
        flash[:notice] = "You have signed up successdully"
        flash[:color] = "valid"
    else
        flash[:notice] = "Invalid Input!"
        flash[:color] = "invalid"
    end
  end

    def user_params
      params.require(:username).permit(:email, :password, :password_confirmation)
    end     

    render "new"

end

用户新视图(new.html.erb)就是这样。

<% @page_title = "UserAuth | Signup" %>
<div class="Sign_Form">
  <h1>Sign Up</h1>
  <%= form_for(:user, :url => {:controller => 'users', :action => 'create'}) do |f| %>
    <p> Username:</br> <%= f.text_field :username%> </p>
    <p> Email:</br> <%= f.text_field :email%> </p>
    <p> Password:</br> <%= f.password_field :password%></p>
    <p> Password Confirmation:</br> <%= f.password_field :password_confirmation%> </p>
    <%= f.submit :Signup %>
  <% end %>
  <% if @user.errors.any? %>
    <ul class="Signup_Errors">
    <% for message_error in @user.errors.full_messages %>
      <li>* <%= message_error %></li>
    <% end %>
    </ul>
  <% end %>
</div>

路线(routes.rb)就像这样

Rails.application.routes.draw do
  get 'users/new'
  resources :users

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  root 'home#index'
end

rake route给我以下输出。

   Prefix Verb   URI Pattern               Controller#Action
users_new GET    /users/new(.:format)      users#new
    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PATCH  /users/:id(.:format)      users#update
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
     root GET    /                         home#index

然而,当我尝试从浏览器访问http://localhost/users/new时,出现以下错误。

Showing C:/Sites/expenses/app/views/users/new.html.erb where line #11 raised:

undefined method `errors' for nil:NilClass
Extracted source (around line #11):
9
10
11
12
13
14

    <%= f.submit :Signup %>
  <% end %>
  <% if @user.errors.any? %>
    <ul class="Signup_Errors">
    <% for message_error in @user.errors.full_messages %>
      <li>* <%= message_error %></li>

1 个答案:

答案 0 :(得分:1)

渲染新应该是创建时的错误。

这意味着,如果您在创建操作中有任何错误,@ user对象将与这些错误一起返回到新表单

因此,您可以使用@user.errors.any?

来解决这些错误
 class UsersController < ApplicationController

  def new
    @user = User.new
  end

  def create

    @user = User.new(user_params)

    if @user.save
        flash[:notice] = "You have signed up successdully"
        flash[:color] = "valid"
    else
        flash[:notice] = "Invalid Input!"
        flash[:color] = "invalid"
        render "new"
    end
  end

    def user_params
      params.require(:username).permit(:email, :password, :password_confirmation)
    end     
end