Rails教程,第7章未定义的方法“model_name”

时间:2012-09-27 19:43:38

标签: ruby-on-rails ruby ruby-on-rails-3

我正在使用Michael Hartl教程。在第7章中,我发现了当我启动服务器并运行

时显示的问题
http://localhost:3000/signup

我收到了这条消息:

 NoMethodError in User#new

 Showing C:/rails_project/sample_app/app/views/user/new.html.erb where line #6 raised:

 undefined method `model_name' for NilClass:Class

 Extracted source (around line #6):

 3: 
 4: <div class="row">
 5:   <div class="span6 offset3">
 6:     <%= form_for(@user) do |f| %>
 7: 
 8:       <%= f.label :name %>
 9:       <%= f.text_field :name %>

 Rails.root: C:/rails_project/sample_app
 Application Trace | Framework Trace | Full Trace

 app/views/user/new.html.erb:6:in `_app_views_user_new_html_erb___578921578_24443340'

知道吗?

2 个答案:

答案 0 :(得分:1)

@user可能未被实例化。在users_controller.rb方法中检查def new是否有@user = User.new之类的内容。

答案 1 :(得分:0)

只做

@user = User.new

当您使用尚未定义的变量

时会出现此类错误