在 Ruby on Rails 中获取控制器中 URL 的锚点部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4108082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:33:19  来源:igfitidea点击:

Get anchor part of URL in controller in Ruby on Rails

ruby-on-railsurlanchor

提问by Pioz

Is there a way to get the anchor part of a URL in a controller?

有没有办法在控制器中获取 URL 的锚点部分?

Example: if I type http://www.foo.com/bar#anchor123can I get the string anchor123 in my controller?

示例:如果我输入http://www.foo.com/bar#anchor123可以在我的控制器中获取字符串 anchor123 吗?

回答by True Soft

You can't do that in Rails, because the anchor is not sent to the server. See mikeduncan.com/?s=named+anchors

您不能在 Rails 中这样做,因为锚不会发送到服务器。见mikeduncan.com/?s=named+anchors

回答by Nicolas Blanco

No sorry, it's not possible to retrieve the #anchor from the server-side (in any language).

不抱歉,无法从服务器端(以任何语言)检索 #anchor。

This is a client-side flag to tell the browser to move to a specific position in the page.

这是一个客户端标志,告诉浏览器移动到页面中的特定位置。

But you can use some Javascript in the body to check for an anchor and send it back to the server using an Ajax-call...

但是您可以在正文中使用一些 Javascript 来检查锚点并使用 Ajax 调用将其发送回服务器...

var anchor_value;
var stripped_url = document.location.toString().split("#");
if (stripped_url.length > 1)
  anchor_value = stripped_url[1];