Pergunta

I am currently working on the Ruby on Rails tutorial by Michael Hartl. I am trying to add a page for each user in my database by creating an HTML with embedded ruby page in the views directory. The code for show.html.erb is below:

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

When I add the user to the user_controller.rb file, it looks like this:

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

    def new
    end
  end

When I run the rails server and click on open up the users/1 URL, I get a NameError complaining about an uninitialized constant. The error and trace is below:

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         
.
.
.
.

Please let me know how to go about this because I cannot pass my spec tests with this error. If anyone has any suggestions or insight I would greatly appreciate them.Thank you.

Foi útil?

Solução

Controller file name should be users_controller.rb

As you mentioned in comment you don't have any User model defined. To create model run rails g model user name:string email:string. This will create User model with attributes name and email.

Try Rails scaffolding and see what files it creates(in controller, model and views ignore other files for now) and see the contents of those files. To use scaffold for creating User is as below:

     rails g scaffold user name:string email:string
     rake db:migrate 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top