简体   繁体   English

Rails路由,添加路径名还可以保持旧网址正常工作

[英]rails routing, add path name also keep old urls working

in my routes file, when i change 在我的路线文件中,当我更改时

resources :foobar

to

resources :foobars, path: "foo-bars"

the urls become example.com/foo-bars , example.com/foo-bars/1 etc. this is ok. 该网址成为了example.com/foo-barsexample.com/foo-bars/1等,这是好的。

but how can i also keep the old urls, example.com/foobars , example.com/foobar/3 also working? 但我如何能也保持旧网址, example.com/foobarsexample.com/foobar/3也工作?

i know, i can hardcode it, 我知道,我可以对其进行硬编码,

get "foobars", to: 'foobar#index'
get "foobar/:id", to: 'foobar#show'
...   

but is there a clean way to implement this? 但是有没有一种干净的方法来实现呢?

Define both of them 定义两者

resources :foobars, path: "foo-bars"
resources :foobars, path: "foobars"

EDIT: 编辑:

For custom actions instead of declaring them twice for each path like this, 对于自定义操作,而不是像这样对每个路径声明两次,

resources :foobars, path: "foo-bars"
  collection do
    get 'bulk_new'
    patch 'bulk_create'
    get 'bulk_edit'
    patch 'bulk_update'
  end
end

resources :foobars, path: "foobars"
  collection do
    get 'bulk_new'
    patch 'bulk_create'
    get 'bulk_edit'
    patch 'bulk_update'
  end
end

Create common block and pass it to both resource method calls. 创建公共块并将其传递给两个资源方法调用。

common_block  = lambda do
  collection do
    get 'bulk_new'
    patch 'bulk_create'
    get 'bulk_edit'
    patch 'bulk_update'
  end
end

resources :foobars, path: "foo-bars", &common_block
resources :foobars, path: "foobars", &common_block

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

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