2011-11-27 64 views
0

我正在研究Michael Hartl的RailsTutorial的第11章,並且我試圖在嘗試構建Twitter克隆時嘗試顯示用戶的微博。它是指當它說的NilClass「未定義的方法‘模型名稱’是什麼型號名稱:類這是什麼錯誤信息?

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 
+2

是否有@microposts變量,還是@ user.microposts? –

+0

在用戶控制器中,我們使用user.micropost創建了一個microposts變量。請參閱OP底部的更新。請注意,出於curisoity的原因,我在此視圖中將microposts更改爲user.microposts,現在錯誤消息提示:「users變量顯示爲空,您忘記將will_paginate的collection對象傳遞給該變量嗎?」 – Leahcim

+0

請發佈控制器代碼。 – jdl

回答

1

如果沒有看到完整的源代碼很難分辨出問題所在,但是您可能可以通過c把你的來源打到reference code at GitHub