3

I'm creating a Rails 4.2 app to print "Hello World" on a web page. But what I get while running the page is this error:

Unknown action The action 'hello' could not be found for ApplicationController

These are the files I updated:

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

  def hello
    render text: "hello, world!"
  end
end

routes.rb

Rails.application.routes.draw do
  # You can have the root of your site routed with "root"
  root 'application#hello'
end
Drenmi
  • 8,492
  • 4
  • 42
  • 51
Sara Taha
  • 73
  • 7
  • you will get your answer from this questin . http://stackoverflow.com/questions/4778301/how-do-you-route-an-action-to-the-application-controller-in-rails-3 – huzefa biyawarwala Jan 12 '16 at 04:33
  • usually you define a new controller as `welcome` controller in tails. then you define the `root :to => "welcome#index"` in `route.rb`. – devanand Feb 27 '16 at 13:34

4 Answers4

2

If you're using cloud9 and following the tutorial at www.railstutorial.org. Then you probably have the code right. You just need to push the "run" button at top center in your dev environment. Hint, it's green.

This detail is omitted in the tutorial but by appearance, the edits you make in the ide aren't saved until you click "run". Seems like a pretty low bar to clear on one's own but it took me hours of google and self-doubt. So try that.

gold2th
  • 21
  • 2
1

Use this way.

Rails.application.routes.draw do
  root to: 'application#hello'
end
Gupta
  • 8,882
  • 4
  • 49
  • 59
0

Your configuration for root route is correct, check accurately controller and action names to be exactly ApplicationController and hello.

But ApplicationController is designed as base controller, you should put common logic here, and extend your controllers from it.

If you are using generator for creating controller, it will be done automatically.

You can find more info in official docs.

arogachev
  • 33,150
  • 7
  • 114
  • 117
0

I think the problem is that you're calling render text: ... IE you're not allowing Rails to finish its render process. If it's wrong, I'll gladly delete the answer; the error seems too simple to be syntax based.

  1. Restart server
  2. Do this:

-

#config/routes.rb
root "application#index"

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

#app/views/application/index.html.erb
Hello World

You can read from this answer how your controller doesn't need methods to just load a view.

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147