开发者

Custom Rails route query

I'm putting together a short URL functionality for an app I'm working on an开发者_StackOverflow中文版d now have it working and turning longer URLs into a short URL by base36 encoding the ID of a record pointing to the longer URL, for example:

http://localhost:3000/7ps -> http://localhost:3000/the/long/url

I am struggling to write a route which will intercept requests for the short URL whilst still allowing requests for other valid URLs in the app.

Is there a route I can use which will only target base36 encoded values after the domain?

Thanks for any help!


You can add:

map.encoded ":encoded_url", :controller => :encoded_urls, :action => :please_decode_me

at the end of your routes. Then it shoudl catch everything that is not catched by other routes.


The short answer is "it depends." You can add a route at the end of your routes file as a catch-all. However, if you have any real routes that are one word long, this will eventually fail.

For example: If you have a path that looks like http://localhost:3000/home which is it? Is that the home page or the short URL for object #825062? Any single-word path you have in your app is going to have this issue.

A very easy way around this would be to add a single character as the first directory in your URI.

http://localhost:3000/r/abc123

Or whatever letter you want. Then you can easily map anything that starts with /r/ to your short-URL lookup controller.

map.connect "/r/:short_url", :controller => "controller_name", :action => "name_of_action_that_looks_up_short_urls"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜