문제

Is it possible to create a quick redirect rule in routes.rb to support:

From:

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

To:

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

My current solution is below. Rails does not allow me to parse :id to get the number "123" out of it. Could you help?

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

해결책

You can pass a block to redirect, which allows you to modify the parameters for the redirection:

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

Also see the Rails documentation on redirection.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top