4

I am trying to learn Rails 4 framework and followed official doc

I ran these following commands:

rails new fold

It created a new project

rails generate controller home index

It generate a home_controller and views/home/index.html.erb

Content of home_controller :

class HomeController < ApplicationController
 def index
 end
end

For routing I added following code in routes.rb

 get 'home/index' => 'home#index'

After running the application I am getting :

uninitialized constant ApplicationController

What I am doing wrong here? Here is the Stacktrace

app/controllers/home_controller.rb:1:in `<top (required)>'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `const_get'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `constantize'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'

What is the wrong with the code?

4
  • 1
    Do you have application_controller.rb in app/controllers? If not you should have it. Commented Oct 31, 2015 at 7:56
  • Oops! I removed it. Do I need to include it is it something related to HomeController < ApplicationController Commented Oct 31, 2015 at 7:57
  • Yes you should include it. Commented Oct 31, 2015 at 7:58
  • Where I will get the code for ApplicaitonCOntroller? Commented Oct 31, 2015 at 7:59

4 Answers 4

5

Do you have an application.rb file? This file should be sitting inside of app/controllers. All of the other controllers inherit from this controller. The most minimal setup would look like this:

class ApplicationController < ActionController::Base
end
Sign up to request clarification or add additional context in comments.

Comments

0

Do this:

#app/controllers/home_controller.rb
class HomeController < ActionController::Base
  def index
  end
end

This should fix it temporarily.

--

To fix it permanently, you should add the following file to your app:

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
   def index
   end
end

You should also change your home_controller back to the following:

#app/controllers/home_controller.rb
class HomeController < ApplicationController
...

Comments

0

Your Application controller should contain this

app/controllers/application_controller.rb

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

end

Comments

0

uninitialized constant ApplicationController

You should have application_controller.rb in app/controllers because ApplicationController is practically the class which every other controller in your application is going to inherit from (although this is not mandatory in any mean).

#app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.