假设我必须路由应该做同样的事情:重定向到特定的 URL,并在参数中进行一些替换。
get '/santitizer', to: redirect {|path_params, req| "/new_url/#{URI.encode(req[:my_param].gsub('.', ' '))}}"}
get '/old_santitizer', to: redirect {|path_params, req| "/new_url/#{URI.encode(req[:my_param].gsub('.', ' '))}}"}
我想应用 DRY 而不是两次使用相同的代码,而是使用一些辅助方法来做这样的事情
get '/santitizer', to: redirect {|path_params, req| santitize_helper(path_params, req)}
get '/old_santitizer', to: redirect {|path_params, req| santitize_helper(path_params, req)}
...
def santitize_helper(path_params, req)
"/new_url/#{URI.encode(req[:my_param].gsub('.', ' '))}}"
end
可能吗?