2012-01-16 16 views
0

我正在關注的railstutorial.org tutorial about microposts.遵循Rails教程並獲得Action控制器:異常被捕獲?

我有以下錯誤:

NoMethodError in Pages#home 

Showing /home/alex/apps/sample_app/app/views/shared/_error_messages.html.erb where line #1 raised: 

You have a nil object when you didn't expect it! 
You might have expected an instance of ActiveRecord::Base. 
The error occurred while evaluating nil.errors 
Extracted source (around line #1): 

1: <% if @user.errors.any? %> 
2: <div id="error_explanation"> 
3:  <h2><%= pluralize(@user.errors.count, "error") %> 
4:   prohibited this <%= object.class.to_s.underscore.humanize.downcase %> user from being saved:</h2> 
Trace of template inclusion: app/views/shared/_micropost_form.html.erb, app/views/pages/home.html.erb 

Rails.root: /home/alex/apps/sample_app 

Application Trace | Framework Trace | Full Trace 
app/views/shared/_error_messages.html.erb:1:in `_app_views_shared__error_messages_html_erb__227730281_80967500' 
app/views/shared/_micropost_form.html.erb:2:in `block in _app_views_shared__micropost_form_html_erb___680753694_80558240' 
app/views/shared/_micropost_form.html.erb:1:in `_app_views_shared__micropost_form_html_erb___680753694_80558240' 
app/views/pages/home.html.erb:6:in `_app_views_pages_home_html_erb__571228236_80133470' 

_error_messages.html.erb:

<% if @user.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(@user.errors.count, "error") %> 
     prohibited this <%= object.class.to_s.underscore.humanize.downcase %> user from being saved:</h2> 
    <p>There were problems with the following fields:</p> 
    <ul> 
    <% @user.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %> 

home.html.erb:

<% if signed_in? %> 
    <table class="front" summary="For signed-in users"> 
    <tr> 
     <td class="main"> 
     <h1 class="micropost">What's up?</h1> 
     <%= render 'shared/micropost_form' %> 
     </td> 
     <td class="sidebar round"> 
     <%= render 'shared/user_info' %> 
     </td> 
    </tr> 
    </table> 
<% else %> 
    <h1>Sample App</h1> 

    <p> 
    This is the home page for the 
    <a href="http://railstutorial.org/">Ruby on Rails Tutorial</a> 
    sample application. 
    </p> 

    <%= link_to "Sign up now!", signup_path, :class => "signup_button round" %> 
<% end %> 

_micropost_form.html.erb:

<%= form_for @micropost do |f| %> 
    <%= render 'shared/error_messages', :object => f.object %> 
    <div class="field"> 
    <%= f.text_area :content %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Submit" %> 
    </div> 
<% end %> 

microposts_controller.html.erb:

class MicropostsController < ApplicationController 
    before_filter :authenticate 

    def create 
    @micropost = current_user.microposts.build(params[:micropost]) 
    if @micropost.save 
     flash[:success] = "Micropost created!" 
     redirect_to root_path 
    else 
     render 'pages/home' 
    end 
    end 

    def destroy 
    end 
end 

pages_controller.html.erb

class PagesController < ApplicationController 
    def home 
     @title = "Home" 
    @micropost = Micropost.new if signed_in? 
    end 

    def contact 
     @title = "Contact" 
    end 

    def about 
     @title = "About" 
    end 

    def help 
    @title = "Help" 
    end 
end 

我已經一直在檢查tu一遍又一遍,一切都看起來完全一樣。

任何建議來解決這個問題?

+0

您可以顯示相應的控制器? – 2012-01-16 08:32:22

+0

@ U-DON OK更新了我的問題。 – alexchenco 2012-01-16 08:32:52

回答

2

您的home操作中似乎沒有@user變量。因此,@user是零。假設您完全按照驗證部分進行操作,請嘗試分配:@user = current_user

+0

嘿它工作。奇怪,在教程中沒有提到它?該教程被廣泛認爲是有史以來最好的導軌教程。 – alexchenco 2012-01-16 08:40:11

+0

經過一番搜索之後,我發現在清單10.4中,教程更新了'shared/_error_messages.html。erb'文件有點(http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#code:updated_error_messages_partial)。與之前的清單8.9比較(http://ruby.railstutorial.org/chapters/sign-up#code:errors_partial)。你的局部版本更像8.9,但應該看起來像10.4 – 2012-01-16 08:54:16

+0

非常感謝。現在我明白了。 – alexchenco 2012-01-16 09:04:29

0

@usernil在這一點上:

Extracted source (around line #1): 

1: <% if @user.errors.any? %> 

nil沒有財產errors,因此會拋出異常。

+0

但教程如何顯示完美呈現的頁面。我應該怎麼做才能獲得與本教程中相同的結果? – alexchenco 2012-01-16 08:33:38

+0

你應該確保'@ user'不是零。確保在你的控制器中,你正在創建'@ user'變量來在視圖中讀取。 – 2012-01-16 08:34:46

0

我認爲它與Pages控制器中的home動作有關。你可以發佈Pages#home的代碼嗎?

編輯: 是的,看到你還沒有定義一個用戶 - @user。

0

對於_error_messages.html.erb,全部@users應該是object

這裏是我的_error_messages.html.erb的副本:

<% if object.errors.any? %> 
    <div id="error_explanation"> 
    <h2><%= pluralize(object.errors.count, "error") %> 
     prohibited this <%= object.class.to_s.underscore.humanize.downcase %> 
     from being saved:</h2> 
    <p>There were problems with the following fields:</p> 
    <ul> 
    <% object.errors.full_messages.each do |msg| %> 
     <li><%= msg %></li> 
    <% end %> 
    </ul> 
    </div> 
<% end %>