2011-09-15 30 views
1

我在用戶接收NoMethodError部分應用程序/視圖/共享/ _error_messages.html.erbRails的教程章8.2.3 NoMethodError在用戶#新#新加入後

Showing /Users/gjb/Sites/rails_projects/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 

這裏是我的應用程序/意見/shared/_error_messages.html.erb

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

這裏是我的應用程序/視圖/用戶/ new.html.erb

<h1>Sign up</h1> 

    <%= form_for(@user) do |f| %> 
    <%= render 'shared/error_messages' %> 

    <div class="field"> 
    <%= f.label :name %> 
    <%= f.text_field :name %> 
    </div>  
    <div class="field"> 
    <%= f.label :email %> 
    <%= f.text_field :email %> 
    </div> 
    <div class="field"> 
    <%= f.label :password %> 
    <%= f.password_field :password %> 
    </div> 
    <div class="field"> 
    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="actions"> 
    <%= f.submit "Sign up" %> 
    </div> 
<% end %> 

我已經花了很多時間在這試圖讓它正確,但我感覺卡在這一點上。任何線索都會有幫助。謝謝!

+0

將「@users」更改爲「@user」 – lucapette

回答

2

你偏好的第一行你有<% if @users.errors.any? %>需要是<% if @user.errors.any? %>。你有複數而不是單數,所以你使用了一個你從未設置過的變量。第一次使用實例變量nil而不是引發錯誤。

+0

這不是複數與奇異首次得到我。我應該看到那一個。謝謝! –

+0

@Emily,+1,因爲你被選中:) – apneadiving

2

app/views/shared/_error_messages.html.erb,替換:

<% if @users.errors.any? %> 

有了:

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

事實上,@users變量不存在。

+0

doh!謝謝!我真的應該抓住那個。 –

+0

一般當兩個答案相同時,一個選擇第一個已發佈。那麼,不要介意,保持艾米莉的(但記得+1存在:)) – apneadiving

+0

對不起,這裏noob。 –

相關問題