Вопрос

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