I'd like to have all my Rails routes treat dasherized paths as equivalent to underscored paths.
i.e. navigating to /foo-bars
and /foo_bars
both trigger the route resources :foo_bars
.
Is that possible?
答案 0 :(得分:1)
这会有所帮助。
type_regexp = Regexp.new(["foo_bars", "foo-bars"].join("|"))
resources "foo_bars", path: type_regexp
如果您有REST以外的路线,请执行此操作
resources "foo_bars", path: type_regexp do
member do
.....
end
collection do
.....
end
答案 1 :(得分:0)
只要定义Athar的答案,因为lambda应该足够好恕我直言
这样的事情:
custom_resources = ->(route_name) {
string_route_name = route_name.to_s
underscore_route_name = string_route_name.underscore
dasherized_route_name = string_route_name.dasherize
path_regexp = Regexp.new([
Regexp.escape(underscore_route_name),
Regexp.escape(dasherized_route_name),
].join('|'))
# more custom code here if desired
resources underscore_route_name, path: path_regexp
}
custom_resources.call(:foo_bar)
custom_resources.call(:another_foo_bar)
警告:未进行实际测试