简体   繁体   English

如何在Rails中没有控制器和动作的情况下将根路径设置为“ /”?

[英]How do I set root path to '/' without controller and action in Rails?

In my routes.rb I want to set the root_path to '/' without specifying any controller or action. 在我的routes.rb中,我想将root_path设置为“ /”而不指定任何控制器或动作。 Is that possible? 那可能吗?

I tried root to: '/' but it's complaining, that no controller or action is specified. 我尝试以root to: '/'登录root to: '/'但它抱怨没有指定控制器或动作。

Updated Info: 更新信息:

I'm using the comfortable-mexican-sofa CMS, and my root_path should go to the cms root path. 我使用的是舒适的墨西哥沙发 CMS,我的root_path应该转到cms的根路径。

At the moment I'm just using redirect_to '/' in my controllers and this is working, but I thought it would be nicer to use root_path . 目前,我只是在控制器中使用redirect_to '/' ,这是可行的,但我认为使用root_path会更好。

Try 尝试

root :to => "cms/content#show"

From the docs: https://github.com/comfy/comfortable-mexican-sofa/wiki/Pages 从文档中: https : //github.com/comfy/comfortable-mexican-sofa/wiki/Pages

The first page you create is the homepage, and therefore you cannot specify a slug. 您创建的第一页是主页,因此您无法指定子弹。 By default, you should be able to access the homepage without specifying your root route. 默认情况下,您应该能够访问主页而无需指定根路由。 However, if you or a gem you are using need access to the root_path helper, specify root :to => "cms/content#show" in your routes. 但是,如果您或您使用的宝石需要访问root_path帮助器,请在路由中指定root :to => "cms/content#show"

Further to the accepted answer, the routes of Rails is all about creating the paths for your application 除了接受的答案,Rails的routes全都涉及为您的应用程序创建路径

According to the ActionDispatch::Routing middleware documentation: 根据ActionDispatch :: Routing中间件文档:

The routing module provides URL rewriting in native Ruby. 路由模块提供本机Ruby中的URL重写。 It's a way to redirect incoming requests to controllers and actions. 这是一种将传入请求重定向到控制器和操作的方法。 This replaces mod_rewrite rules. 这将替换mod_rewrite规则。 Best of all, Rails' Routing works with any web server. 最重要的是,Rails的路由可与任何Web服务器一起使用。 Routes are defined in config/routes.rb 路由在config / routes.rb中定义

This means the job of routes.rb is to take the inbound request ( / , /posts , etc) & redirect to ruby-centric code. 这意味着routes.rb的工作是接routes.rb站请求( //posts等)并重定向到以Ruby为中心的代码。 Ruby doesn't know what / is -- it has to have a controller / action combo 红宝石不知道是什么/是-它必须有一个controller / action组合

That's why you'd need something like: 这就是为什么您需要类似以下内容的原因:

#config/routes.rb
root to: "controller#action"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM