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

时间:2018-12-23 03:52:56

标签: ruby-on-rails ruby routes

我有Rails 5.1应用程序,并且在controller do块下有一些路由。

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

如您所见,所有路由名称实际上都映射到相同的命名动作。我正在寻找将其干燥的任何可能性。与此类似。

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

请考虑我希望这些路由为/dashboard而不是/pages/dashboard,这是不使用常规路由(即脚手架生成的get /pages/dashboard)的原因。

1 个答案:

答案 0 :(得分:1)

像这样:

Pages#dashboard

这会将/ dashboard路由到bundle exec rake routes | grep dashboard,您可以使用class Site: def save(self): pic = DailyPic() def filename(pic_self): val = self.mk_filename(pic_self) pic_self.filename = lambda _: val return val pic.filename = filename return pic 进行验证