在config / routes.rb中的rack helper

时间:2013-10-19 17:05:09

标签: ruby-on-rails routing url-routing rack helper

假设我必须执行相同操作的路由:使用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

有可能吗?

0 个答案:

没有答案