修改params以在Ruby on Rails中重定向路由

时间:2014-05-14 19:36:52

标签: ruby-on-rails ruby ruby-on-rails-3 routes

是否可以在routes.rb中创建快速重定向规则以支持:

自:

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?/}

1 个答案:

答案 0 :(得分: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