1
votes

I'm new in Ruby and follow the coder-manual course

I have this error NameError Rails in Controller NameError in ContactsController#new uninitialized constant ContactsController::Contact

here is my Contacts_controller.rb

class ContactsController < ApplicationController
    def new
        @contact = Contact.new
    end

    def create
    end
end

and my view in folder app -> views -> contacts -> new.html.erb

<div class="row">
    <%= form_for @contact do |f| %>
    <% end %>
</div>

What am I missing ?

2
do you have a class Contact? - Andrey Deineko
Could you please show contact.rb file? It is located inside app/models folder, if there at all of course. I have a feeling it either doesn't exist or the class name inside of the file is changed and thus causing an error. - Timur Mamedov
Oh yes...I don't have a model...thanks everyone... - Nina

2 Answers

1
votes

From the error it sounds like you're missing a model. The easiest way to create a model is through a rails generator. In your terminal navigate to the root of your project. And type

rails generate model Contact

If you wanted to add attributes to the Contact model through the generator you would type something like...

rails generate model Contact name:string age:integer on_facebook:boolean

Otherwise if you wanted to create the controller, model and restful routes you could type

rails generate resource Contact name:string age:integer

That'll create the files you need and update your routes.

0
votes

You should create a Contact model also.

class Contact < ActiveRecord::Base
  # Some code
end

Hope this helps.