Is it possible to remove the default root in a Rails application without creating a new one?

When creating a new Rails application, by default it serves the Welcome to Rails page in / unless you specify an alternative root in routes.rb .

My application currently only services things from a subpath (e.g. /api/v1/ ), so access to / should result in 404. How can I accomplish this?

+8
source share
2 answers

If you want to answer 404 , I can come up with two approaches.

First, you can forward to Rack and return a simple 404 answer:

 # config/routes.rb root to: proc { [404, {}, ["Not found."]] } 

Secondly, you can choose the obvious route and point root to the action of the controller, which 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, of course, is to go over to a non-existent action, but I donโ€™t think itโ€™s a good idea, because the intention is hidden and can easily be mistaken for an error.

+9
source

If you delete it, you will see the default greeting from Rails, which includes some technical information about your environment. This is hardly the behavior you need.

I solved it like this:

 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 
+1
source

Source: https://habr.com/ru/post/1235284/


All Articles