Leahcim
Leahcim

Reputation: 42049

What is this error message?

I'm working on chapter 11 of Michael Hartl's RailsTutorial and I got this error message trying to show the user's microposts while trying to build a Twitter clone. What model name is it referring to when it says "Undefined method 'model_name' for NilClass:Class?

undefined method `model_name' for NilClass:Class

Extracted source (around line #10):

7:       </h1>
8:     <% unless @user.microposts.empty? %>
9:         <table class="microposts" summary="User microposts">
10:           <%= render @microposts %>
11:         </table>
12:         <%= will_paginate @microposts %>
13:       <% end %>

Edit In the action I set @microposts:

@microposts = @user.microposts.paginate(:page => params[:page])

*Edit*Controller code

class UsersController < ApplicationController

before_filter :authenticate, :only => [:index, :edit, :update]
before_filter :correct_user, :only => [:edit, :update]
before_filter :admin_user, :only => :destroy

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(:page => params[:page])
    @title = @user.name
  end

  def new
    @user = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      @title = "Sign up"
      render 'new'
    end
  end

   def edit

    @title = "Edit user"
   end

   def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "Profile updated."
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end

  def index
    @title = "All users"
    @users = User.paginate(:page => params[:page])
  end

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

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User destroyed."
    redirect_to users_path
  end

  private

    def authenticate
      deny_access unless signed_in?
    end

     def correct_user
      @user = User.find(params[:id])
      redirect_to(root_path) unless current_user?(@user)
    end

     def admin_user
      redirect_to(root_path) unless current_user.admin?
    end

end

Upvotes: 0

Views: 731

Answers (1)

mhartl
mhartl

Reputation: 1931

It's hard to tell what the problem is without seeing the full source code, but you might be able to track down the problem by comparing your source to the reference code at GitHub.

Upvotes: 1

Related Questions