1

In my rails app I am getting the following error:

NameError in ProjectsController#index
app/controllers/projects_controller.rb:6:in `index'

My routes look like this:

RailsStarter::Application.routes.draw do
resources :projects

root :to => 'pages#home'
match '/' => 'pages#home'
match '/overview' => 'pages#overview'
match '/recruitmentInfo' => 'pages#recruitmentInfo'

And the problem controller:

 class ProjectsController < ApplicationController

 # GET /projects
 # GET /projects.json
 def index
 @projects = ::Project.all

 respond_to do |format|
   format.html # index.html.erb
   format.json { render json: @projects }
 end
end

My investigations so far showed that Project is not yet initialized, but this shouldn't matter as it is a constructor, in its own class. I even tryed precompiling that by adding this line config.autoload_paths += %W( #{config.root}/app/controllers) to application.rb. Nothing seems yet to work. Any ideas will be greatly appreciated. Thanks!

Vlad Balanescu
  • 664
  • 5
  • 27
  • 1
    Why don't you go with `@projects = Project.all`? You don't really need the namespace resolution operator? – Arun Kumar Mohan Oct 29 '16 at 14:04
  • That is not making any difference apart from throwing the error `uninitialized constant ProjectsController::Project` – Vlad Balanescu Oct 29 '16 at 14:05
  • Well, It means that there is no `Project` class defined. Did you generate the `Project` model? – Arun Kumar Mohan Oct 29 '16 at 14:06
  • No, because I used a scaffold and I expected to generate the model as well for me – Vlad Balanescu Oct 29 '16 at 14:10
  • When I do `rails generate model Project id:integer primary_key name:string` the command is returning 0, no errors, but that's not generating my model – Vlad Balanescu Oct 29 '16 at 14:13
  • Did you run `rake db:migrate` after generating the scaffold? Btw, why are you creating a field called `primary_key` of string type? You might want to read up on the syntax of rails model generators. And you can't redefine the id field. its created for you. – Arun Kumar Mohan Oct 29 '16 at 14:16
  • Yes, I did run rake db:migrate. After the scaffold I don't get any entry in my `db/` folder and when I run `rake db:migrate` it comes up with `Don't know how to build task 'db:migrate' (see --tasks)` – Vlad Balanescu Oct 29 '16 at 14:19
  • If you're working with rails 5, please try `rails db:migrate` – Arun Kumar Mohan Oct 29 '16 at 14:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/126962/discussion-between-vlad-balanescu-and-arun-kumar). – Vlad Balanescu Oct 29 '16 at 15:10

1 Answers1

0

[SOLVED]:

1 Reboot machine - the command must interfere with another server process
2 Check active_support gem versions by running `gem list` and remove them all
3 Remove `Gemfile.lock`
3 Run `bundle install` - this will add back your new version of gemfile
4 Run `rails generate model Project` again

This should now be fixed!

Vlad Balanescu
  • 664
  • 5
  • 27