且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Ruby on Rails 中 # 后提取 url 中的字符串

更新时间:2023-02-23 10:40:58

您不能在服务器端执行此操作.URL 片段标识符不会发送到服务器.

You cannot do this on the server side. URL fragment identifiers are not sent to the server.

然而,您可以使用 JavaScript 在客户端捕获此值,并向服务器发送 Ajax 请求并传递上述值.

You can however trap this value on the client side with JavaScript and send an Ajax request to the server passing the aforementioned value.

jQuery 中的一个实现?

An impl in jQuery perchance?

$(function() {
   $.post('someRailsEndpoint', {hash: document.location.hash});
});

在 Rails 端,您将使用 params[:hash] 访问此值.

On the Rails end, you'd use params[:hash] to access this value.