Rails — Params with “dot” (e.g. /google.com)

前端 未结 4 781
情歌与酒
情歌与酒 2020-12-09 04:02

How to force Rails to consider a param with a dot in the value like google.com (e.g. /some_action/google.com) a single param and not \"id\" =

4条回答
  •  眼角桃花
    2020-12-09 04:09

    We had similar case when we removed some part of an api path. Basically we went from /api/app/v1/* to /api/v1/*

    We put this in our routes

    match '/api/app/v1/*path', to: redirect(path: '/api/v1/%{path}'), via: :all
    

    This was all fine except for some routes that ended with path params including dots. E.g. /api/v1/foo/00.00.100 where .100 got parsed into format and the remaining param only had the value 00.00

    We guarded this with some constraint on the params.

    put '/api/app/v1/foo/:version', 
        constraints: { version: /([0-9]+)\.([0-9]+)\.([0-9]+)/ },
        to: redirect('/api/v1/foo/%{version}')
    

    Edit: we use rails 5

提交回复
热议问题