Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I remove the default root in a Rails application without creating a new one?

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?

like image 229
DanielGibbs Avatar asked Nov 05 '15 10:11

DanielGibbs


2 Answers

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.

like image 112
Drenmi Avatar answered Sep 29 '22 20:09

Drenmi


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
like image 37
Artur Beljajev Avatar answered Sep 29 '22 18:09

Artur Beljajev