When creating a new Rails application, by default it serves a "Welcome to Rails" page at /
unless you specify an alternative root
in routes.rb
.
My application currently only serves things from a subpath (e.g. /api/v1/
) so accessing /
should result in a 404. How can I accomplish this?
If you want to render a 404
response, there are two approaches that I can think of.
Firstly, you could route to Rack, and return a simple 404
response:
# config/routes.rb
root to: proc { [404, {}, ["Not found."]] }
Secondly, you could take the obvious route and point root to a controller action that returns 404
:
# config/routes.rb
root to: "application#not_found"
# app/controllers/application_controller.rb
def not_found
render plain: "Not found.", status: 404
end
The third option is, of course, to route to a non-existing action, but I don't think this is a good idea, since the intention is obscured, and could easily be taken for a mistake.
If you remove it, you will see the default greeting from Rails, which includes some technical information about your environment. Unlikely this is the behavior you need.
I solved it this way:
Rails.application.routes.draw do
namespace :admin do
...
end
# To prevent users from seeing the default welcome message "Welcome aboard" from Rails
root to: redirect('admin/sign_in')
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With