开发者

Rails routing to root

开发者 https://www.devze.com 2023-02-14 13:26 出处:网络
When setting up the root route, is there any compelling reason to choose this syntax: map.r开发者_运维百科oot :controller => \"login\", :action => \'show\'

When setting up the root route, is there any compelling reason to choose this syntax:

map.r开发者_运维百科oot :controller => "login", :action => 'show'

over this syntax:

match "/" => "login#show"

The second syntax will allow you to use the :constraints option, where the first wont. Is there any reason to use the first option?


When you use root :to rails 3 automatically creates the helper methods root_url and root_path for referencing your application root. These methods are often used in gems to reference your applications root and I'm not actually sure where these would point or if they would even work if you don't specify anything (never tried it). Plus it's the "rails way" of doing things so it's usually best to follow unless you have a really good reason.


I believe root routes should be set up as follows:

root :to => "Something#index"

The methods you suggested sound like they may cause conflicts later on down the road.


In Rails 4, here's a quicker code you can use:

 root 'login#new_session'

You can substitute new_session with show/index/etc, just make sure to define it in your login controller.


I think the following two are the same:

root :to => 'login#show'

match '/' => 'login#show', :as => :root

Just like other paths, if you want a root_path, then for the match '/', you have to specify it by yourself.

So I think they just do the same thing (routing you to login#show if the path is /), but the first one would have more semantic meaning.

0

精彩评论

暂无评论...
验证码 换一张
取 消