假设我必须执行相同操作的路由:使用params中的一些替换重定向到特定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
有可能吗?