How to match a literal colon in a route

已查看 84 次
跳至第一个未读帖子

digger250

未读,
2016年2月29日 18:04:022016/2/29
收件人 Ruby on Rails: Talk
I'm trying to figure out how to escape a literal colon in the routes. If I make a route like this:

get '/thing:/:id' => 'things#show'

then '/thing:/123' does not match on this route

I've also tried '/thing\:/:id' and '/thing%3A/:id' none seem to work.

Does anyone have any tips here?

-Justin

Hassan Schroeder

未读,
2016年2月29日 18:46:102016/2/29
收件人 rubyonrails-talk
Yes - don't do that :-)

See https://www.ietf.org/rfc/rfc1738.txt, specifically Section 2.2 on
reserved characters.

--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Justin Coyne

未读,
2016年2月29日 20:21:192016/2/29
收件人 rubyonra...@googlegroups.com
I'd certainly prefer not to, but it is prescribed by the ARK standard:  https://en.wikipedia.org/wiki/Archival_Resource_Key

-Justin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yDceABV646sh%2BH5L4uWoW%3D1Scc2ucj6ta4tYLU_q8JmRg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Matt Jones

未读,
2016年3月2日 16:46:402016/3/2
收件人 Ruby on Rails: Talk、jco...@justincoyne.com
I did some digging on this, and I couldn't find a way to make this work with a literal in the route itself. 

Splatting works, though:

  get 'baz/*ark_tag/:id/bar' => 'welcome#index', constraints: { ark_tag: 'ark:' }, as: :ark

will only match paths like "/baz/ark:/123/bar".

It's a little fussy with URL helpers, as you need to always specify ark_tag or you'll get an error:

  ark_path(ark_tag: 'ark:', id: 1234)

You'll probably want to define your own helpers that hide that.

The other, bigger issue is the OTHER features of the ARK spec that are going to confuse & annoy the default URL parser. In particular:

* the dot-separated components of VariantPath elements. The default Rails behavior appends a `.:format` optional parameter to the route. This *doesn't* allow further dots.

* the `?` and `??` suffixes. The single question-mark, in particular, is only detectable if you check the original request URI in the Rack request. I'm also unsure how to talk the regular path helpers into producing a single-question-mark URL.

If you're building an application which relies on complex routing of ARK URLs, you may want to consider writing your own Rack middleware to hijack the URL parsing process and transmute them into something more palatable to the existing router.

--Matt Jones
回复全部
回复作者
转发
0 个新帖子