这个错误信息是什么?

时间:2011-11-27 02:40:01

标签: ruby-on-rails

我正在研究Michael Hartl的RailsTutorial的第11章,我在尝试构建Twitter克隆时尝试显示用户的微博时收到此错误消息。当它为NilClass说“未定义的方法'model_name'时,它指的是什么型号名称: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 %>

编辑在我设置@microposts

的操作中
@microposts = @user.microposts.paginate(:page => params[:page])

*编辑*控制器代码

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

1 个答案:

答案 0 :(得分:1)

如果没有看到完整的源代码,很难说出问题所在,但您可以通过将源与reference code at GitHub进行比较来追踪问题。