'Errors' Partial works for 'New' user but not 'Edit' user

I'm learning Ruby on Rails and 100% newb to coding. Currently, I am getting this message:

Extracted source (around line #1): undefined method `errors' for nil:NilClass

<% if obj.errors.any? %>

I only get this message in my edit.html.erb:

<%= render 'shared/errors', obj: @user %>

<div class='row'>
    <div class='col-xs-12'>
        <%= form_for(@user, :html => {class: "form-horizontal", role: "form"}) do |f| %>
            <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :username %>
                </div>
                <div class="col-sm-8">
                <%= f.text_field :username, class: "form-control", placeholder: "Enter Username", autofocus: true %>
                </div>
            </div>
            <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :email %>
                </div>
                <div class="col-sm-8">
                    <%= f.email_field :email, class: "form-control", placeholder: "Enter Email"  %>
                </div>
            </div>
                  <div class="form-group">
                <div class="control-label col-sm-2">
                    <%= f.label :password %>
                </div>
                <div class="col-sm-8">
                    <%= f.password_field :password, class: "form-control", placeholder: "Enter Password"  %>
                </div>
            </div>
            <div class = "form-group">
                <div class = "col-sm-offset-2 col-sm-10">
                    <%= f.submit 'Sign Up', class: "btn btn-primary btn-md" %>
                </div>
            </div>
        <% end %>

        <div class = "col-xs-4 col-xs-offset-5">
            [ <%= link_to 'Back to Index', articles_path %> ]
        </div>
</div>
    </div>

My new.html.erb has the exact same code but does not get the error message. This is the UsersController:

class UsersController < ApplicationController

   def new 
      @user = User.new 
   end

   def create
       @user = User.new(user_params)
       if @user.save
           flash[:success] = "Welcome to Alpha Blog #{@user.username}"
           redirect_to articles_path
       else
           render 'new'
        end

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

    def update

    end
   end


    private

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

This is the errors partial:

<% if obj.errors.any? %>
    <div class="row">
        <div class="col-xs-8 col-xs-offset-2">
            <div class ="panel panel-danger">
                <div class="panel-heading">
                    <h2 class ="panel-title">
                    <%= pluralize(obj.errors.count, "error") %>
                    prohibited this article from being saved:
                    </h2>
                    <div class ="panel-body">
                        <ul>
                             <% obj.errors.full_messages.each do |msg| %>
                                <li><%= msg %></li>
                             <% end %>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
<% end %>

What am i doing wrong? Thanks in advance!


Not sure why you are doing a partial for this when there are plenty of gems and helpers to do this for you.

The problem is, you forgot locals and partial, as you can read in the Rails guide.

<%= render partial: 'shared/errors', locals: {obj: @user} %>

Personally I'd recommend to change obj to object, in rails we like to write-out words.


It seems your update action is empty I mean no any valid code for the action, you can add some valid code for updating like below

def update
    @user = User.find(params[:id])
    respond_to do |format|
        if @user.update_attributes(user_params)
            flash[:success] = 'User was successfully updated.'
            format.html { redirect_to @user }
            format.xml  { head :ok }
        else
            format.html { render :action => "edit" }
            format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
        end
    end
end 

Now try to check validation using incorrect data.


It turns out i closed the "if" block in the Create Method but didn't close the Create Method itself.