【问题标题】:Modify params to redirect in route in Ruby on Rails修改参数以在 Ruby on Rails 中重定向路由
【发布时间】:2014-05-14 19:36:51
【问题描述】:

是否可以在 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?/}

【问题讨论】:

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


    【解决方案1】:

    您可以将一个块传递给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

    【讨论】:

    • 效果很好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多