NoMethodError in Users#index

1.8k views Asked by At

I have a problem with the following application, I have been doing the episode #211 Validations in Rails 3 of railsCasts, but when I try add the class EmailFormatValidator a error is triggered

NoMethodError in Users#index

Showing /home/fernando/Validations/store/app/views/users/index.html.erb where line #12 raised:

undefined method `each' for nil:NilClass
Extracted source (around line #12):

9:     <th></th>
10:   </tr>
11: 
12: <% @users.each do |user| %>
13:   <tr>
14:     <td><%= user.name %></td>
15:     <td><%= user.email %></td>
Rails.root: /home/fernando/Validations/store

models/user.rb

class User < ActiveRecord::Base
  validates :email, :presence => true, :uniqueness => true, :email_format => true
end

lib/email_format_validator.rb

class EmailFormatValidator < ActiveModel::EachValidator
  def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
      object.errors[attribute] << (options[:message] || "is not formatted properly") 
    end
  end
end

/controller/user.rb

class UsersController < ApplicationController
  # GET /users
  # GET /users.json
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  # GET /users/1
  # GET /users/1.json
  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

# GET /users/new # GET /users/new.json def new @user = User.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @user }
end

end

# GET /users/1/edit def edit @user = User.find(params[:id]) end

# POST /users # POST /users.json def create @user = User.new(params[:user])

respond_to do |format|
  if @user.save
    format.html { redirect_to @user, notice: 'User was successfully created.' }
    format.json { render json: @user, status: :created, location: @user }
  else
    format.html { render action: "new" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

end

# PUT /users/1 # PUT /users/1.json def update @user = User.find(params[:id])

respond_to do |format|
  if @user.update_attributes(params[:user])
    format.html { redirect_to @user, notice: 'User was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
end

end

# DELETE /users/1 # DELETE /users/1.json def destroy @user = User.find(params[:id]) @user.destroy

respond_to do |format|
  format.html { redirect_to users_url }
  format.json { head :no_content }
end

end end

This error appears when I click one time ArgumentError in UsersController#index

Unknown validator: 'EmailFormatValidator'
Rails.root: /home/fernando/Validations/store
1

There are 1 answers

1
Mehul Gurjar On BEST ANSWER

Please create validators dir inside app folder.

Your file name should be look like email_format_validator.rb inside validators folder

#app/model/user.rb
class User < ActiveRecord::Base
  validates :email, :presence => true, :uniqueness => true, :email_format => true
end

Please remove this validator file from "lib folder".

OR

Simple way to write Rails validation for e-mail with Devise

More simple validates :email, :presence => true, :format => Devise.email_regexp