Extract string in url after # in Ruby on Rails

2019-07-29 12:23发布

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?

1条回答
Luminary・发光体
2楼-- · 2019-07-29 12:54

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

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

An impl in jQuery perchance?

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

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

查看更多
登录 后发表回答