简体   繁体   English

传统上如何将控制器动作映射到导轨中的路由?

[英]How to conventionally map controller actions to routes in rails?

I have rails 5.1 application and have some set of routes under controller do block. 我有rails 5.1应用程序,并且在controller do block下有一些路由。

controller "pages" do 
    get "/dashboard", action: "dashboard"
    get "/guide", action: "guide"
    get '/welcome/:email', action: "welcome"
    get '/guest/:email', action: 'guest'
    get '/settings', action: "settings"
end

As you can see all routes name are actually mapping to same named action. 如您所见,所有路由名称实际上都映射到相同的命名动作。 I am looking for any possibility to DRY it up. 我正在寻找将其干燥的任何可能性。 Something similar to this. 与此类似。

controller "pages" do 
    get "/dashboard"
    get "/guide"
    get '/welcome/:email'
    get '/guest/:email'
    get '/settings'
end

Please consider that I want the routes as /dashboard and not /pages/dashboard which is the reason of not using the conventional routes ie get /pages/dashboard generated by scaffold. 请考虑我希望将路由作为/dashboard而不是/pages/dashboard ,这是不使用常规路由的原因,即get /pages/dashboard由脚手架生成的get /pages/dashboard

like so: 像这样:

controller "pages" do 
    get :dashboard
end

this will route /dashboard to Pages#dashboard which you can verify with bundle exec rake routes | grep dashboard 这会将/ dashboard路由到Pages#dashboard ,您可以使用bundle exec rake routes | grep dashboard进行验证bundle exec rake routes | grep dashboard bundle exec rake routes | grep dashboard

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

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