我在用戶接收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 %>
我已經花了很多時間在這試圖讓它正確,但我感覺卡在這一點上。任何線索都會有幫助。謝謝!
將「@users」更改爲「@user」 – lucapette