2014-05-14 80 views
0

是否有可能創造routes.rb中快速重定向規則支持:修改PARAMS在Ruby中的路線重定向on Rails的

來源:

http://example.com/family/uid-123-child 

要:

http://example.com/family/parent?child=123 

我目前的解決方案如下。 Rails不允許我解析:id從中獲取數字「123」。你能幫忙嗎?

get "family/:id", to: redirect('/parent?child='.concat('%{id}')), :constraints => { :id => /[A-Za-z0-9_\-:]+\child?/} 

回答

0

你可以傳遞一個塊redirect,它允許你修改參數重定向:

get "family/:id", to: redirect { |params, request| "/family/parent?child=#{params[:id][/[0-9]+/]}" }, constraints: { id: /[A-Za-z0-9_\-:]+child?/ } 

另見Rails documentation on redirection

+0

工程很棒。非常感謝。 – kt2k

+0

不客氣! – fivedigit