在 Ruby on Rails 中 # 后提取 url 中的字符串 [英] Extract string in url after # in Ruby on Rails

查看:24
本文介绍了在 Ruby on Rails 中 # 后提取 url 中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我目前使用二维码,我无法更改请求 url.我需要能够解析地址以获取哈希符号后的字符串,即:http://domain.com/#getthisstring有没有办法做到这一点?

Due to my current implementation of using QR Codes, I cannot change the request url. I need to be able to parse an address to get the string after the hash sign, i.e.: http://domain.com/#getthisstring Is there a way to do this?

推荐答案

您不能在服务器端执行此操作.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.

这篇关于在 Ruby on Rails 中 # 后提取 url 中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆