[英]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
. 在创建新的Rails应用程序时,默认情况下它会在
/
处提供“欢迎使用Rails”页面,除非您在routes.rb
指定备用root
。
My application currently only serves things from a subpath (eg /api/v1/
) so accessing /
should result in a 404. How can I accomplish this? 我的应用程序目前只从子路径(例如
/api/v1/
)提供服务,因此访问/
应该产生404.我如何实现这一目标?
If you want to render a 404
response, there are two approaches that I can think of. 如果你想呈现
404
响应,我可以想到两种方法。
Firstly, you could route to Rack, and return a simple 404
response: 首先,您可以路由到Rack,并返回一个简单的
404
响应:
# 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
: 其次,您可以采用明显的路径并将root指向返回
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. 如果删除它,您将看到Rails的默认问候语,其中包含有关您的环境的一些技术信息。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.