问题描述:

When I typed the code below in app/controllers/application_controller.rb:

class ApplicationController < ActionController::Base

protect_from_forgery with: :exception

def hello

render html: "hello, world!"

end

end

and the code below in config/routes.rb:

Rails.application.routes.draw do

root 'application#hello'

end

The root route still returns the default Rails page while I was expecting it would return "hello, world!". Please help me with this small issue.

网友答案:

I just built a test app and used your code above...it worked great! I went to http://localhost:3000 and successfully got the "hello, world!" message.

What version of rails are you using?

What happens when you run rake routes (or rails routes if you're on 5+) from the command line? Mine looks like this:

$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET  /           application#hello

Note - you might need to restart your rails server depending on how you have everything set up but if your server is running on your laptop, the reboot should have handled that.

相关阅读:
Top