當我訪問http://localhost:3000/users時,它給我NoMethodError in Users#index
。該錯誤是如下:Rails 3中未定義的方法/ NoMethodError 3
NoMethodError in Users#index
Showing /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/todo/app/views/users/index.html.erb where line #2 raised:
undefined method `name' for nil:NilClass
Extracted source (around line #2):
1: <% @users.each do |user| %>
2: hello!! <% @user.name %>
3: <% end %>
Rails.root: /Applications/XAMPP/xamppfiles/htdocs/rails_projects/TUTORIALS/todo
Application Trace | Framework Trace | Full Trace
app/views/users/index.html.erb:2:in `block in _app_views_users_index_html_erb___3451413751309771876_2169723860_4451603425222664605'
app/views/users/index.html.erb:1:in `each'
app/views/users/index.html.erb:1:in `_app_views_users_index_html_erb___3451413751309771876_2169723860_4451603425222664605'
我的模型user.rb:
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime
# updated_at :datetime
#
class User < ActiveRecord::Base
attr_accessible :name, :email
我的視圖應用/視圖/用戶/ index.html.erb:
<% @users.each do |user| %>
hello!! <% @user.name %>
<% end %>
我的控制器應用/controllers/users_controller.rb
我的路線。 RB文件有:
resources :users
我所有的測試都通過了(使用RSpec的),包括測試用例規格/控制器/ users_controller_spec.rb:
describe "GET 'index'" do
it "should be successful" do
get 'index'
response.should be_success
end
end
當我訪問http://localhost:3000/users/1,它讓我看到的用戶完美。代碼在應用程序/視圖/用戶/ show.html.erb:
<p>
<b>Name:</b>
<%= @user.name %>
</p>
我已經做了耙分貝:測試:準備,我認爲錯誤鏈接到數據庫或遷移。任何想法?謝謝!
如果你向我們展示了你所得到的NoMethodError,它會幫助我們。 – 2011-05-31 03:35:56
@Ryan:謝謝!剛剛在我的問題開始時添加了錯誤!我做了rake db:reset,然後rake db:migrate,rake db:test:prepare。它仍然顯示錯誤! – Sayanee 2011-05-31 06:53:17