Ruby-on-rails root_path 或 root_url 在哪里?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7504053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 02:00:32  来源:igfitidea点击:

Where's root_path or root_url?

ruby-on-railsroutes

提问by Lai Yu-Hsuan

In rails, there are a lot of helpers to direct our actions, like collections_pathnew_member_pathedit_member_pathand so on.

在 Rails 中,有很多助手来指导我们的行动,诸如此类collections_pathnew_member_pathedit_member_path

But where is the root? Is there a helper always points to my homepage?

但根在哪里?有帮手总是指向我的主页吗?

回答by Benoit Garret

These helpers exist, but you have to set your root url before, in config/routes.rb:

这些助手存在,但你必须在之前设置你的根 url,在config/routes.rb

root :to => "controller#action"

You can use root_urland root_pathafterwards.

您可以使用root_urlroot_path之后。

Be warned that there's a catch when doing redirections with the _pathhelpers, you should use the _urlones when redirecting (see @LanguagesNamedAfterCoffee's comment for the details).

请注意,使用_path助手进行重定向时有一个问题,您应该_url在重定向时使用它们(有关详细信息,请参阅@LanguagesNamedAfterCoffee 的评论)。