2013-03-27 23 views
2

我目前正在使用Michael Hartl的Ruby on Rails教程。我试图通过在views目录中创建一个嵌入了ruby页面的HTML来为我的数据库中的每个用户添加一个页面。对于show.html.erb的代码如下:UsersController中的NameError#show

<%= @user.name %>, <%= @user.email %> 

当我将用户添加到user_controller.rb文件,它看起来像这样:

class UsersController < ApplicationController 
    def show 
     @user = User.find(params[:id]) 
    end 

    def new 
    end 
    end 

当我运行轨道服务器并点击在打开users/1 URL时,我得到一个NameError抱怨未初始化的常量。误差和跟踪如下:

NameError in UsersController#show 
uninitialized constant UsersController::User 

Rails.root: /usr/sample_app 
Application Trace | Framework Trace | Full Trace 

app/controllers/users_controller.rb:3:in `show' 
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action' 
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action' 
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action' 
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action' 
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in   
. 
. 
. 
. 

请让我知道如何去了解这一点,因为我无法通过我的规格测试,此错误。如果有人有任何建议或见解,我将不胜感激他们。谢谢。

+0

那么,你有'应用程序/模型/ user.rb'定义'类User'? – khustochka 2013-03-27 15:23:45

+0

不,我不知道。实际上,我无法在models目录中找到我的user.rb文件。这很奇怪,因为我知道我之前有一个 – Nyce37 2013-03-27 15:25:47

回答

3

控制器文件名应该是users_controller.rb

正如你在评论you don't have any User model defined提及。创建模型运行rails g model user name:string email:string。这将创建User模型,其属性为name and email

尝试导轨scaffolding并查看它创建了哪些文件(目前在controller, model and views中忽略其他文件)并查看这些文件的内容。要创建User使用的支架是如下:

 rails g scaffold user name:string email:string 
    rake db:migrate 
相关问题