简体   繁体   English

将隐藏参数传递给 url

[英]Passing hidden params into url

I have a rails application in which I would like to generate a url based on a parameter, but for that parameter to be hidden from public view.我有一个 Rails 应用程序,我想在其中基于一个参数生成一个 url,但该参数从公众视野中隐藏。 So essentially working like a POST request but being able to be typed in like a GET request.所以本质上像 POST 请求一样工作,但能够像 GET 请求一样输入。

For example using a QR reader I could have the address as www.site.com/qr?lot_no=18007 but when a user scans the QR image it only shows www.site.com/qr but displays the results related to lot_no=18007 .例如,使用 QR 阅读器,我的地址可以是www.site.com/qr?lot_no=18007 ,但是当用户扫描 QR 图像时,它只显示www.site.com/qr ,但显示与lot_no=18007相关的结果.

Not sure if this is possible or not.不确定这是否可能。 But any help would be greatly appreciated.但任何帮助将不胜感激。

If all you want to do is to prevent it from showing up in the address bar of the browser, you could use Rails.ajax to make the request dynamically through Javascript.如果您只想阻止它出现在浏览器的地址栏中,您可以使用Rails.ajax通过 Javascript 动态发出请求。

That will at least hide it from casual inspection, but there is no way to suppress the parameters from the query string on a GET completely, so anyone looking at the Networks tab in the browser (for example) would still see them.这至少会隐藏它以防止随意检查,但没有办法完全抑制 GET 上查询字符串中的参数,因此任何人在浏览器中查看“网络”选项卡(例如)仍然会看到它们。

Another alternative would be to encrypt the parameter value.另一种选择是加密参数值。

Maybe the Friendly id gem may be of help here The following is an example of it's use is也许友好的 id 宝石在这里可能会有所帮助以下是它的使用示例

http://example.com/states/washington

instead of:

http://example.com/states/4323454

This is not going to work with a post request as you mention though.不过,这不适用于您提到的发布请求。 It is a way of using a get request that would normally send an id in the params hash to retrieve existing records.这是一种使用获取请求的方法,该请求通常会在参数 hash 中发送一个 id 来检索现有记录。

The gem can be found here https://github.com/norman/friendly_id It is well maintained and up to date宝石可以在这里找到https://github.com/norman/friendly_id它维护得很好并且是最新的

Configure different route for public facing URL, the URL should include encrypted lot id as a path param.为面向公众的 URL 配置不同的路由,URL 应包含加密的批次 ID 作为路径参数。

routes.rb路线.rb

get '/view_lot/:id' => 'QrCodesController#view_lot`, as: :public_view_lot

now in QrCodesController add action view_lot现在在QrCodesController添加动作view_lot

def view_lot
  encrypted_id = params[:id]
  id = decrypt_it(encrypted_id)
  @lot = Lot.find(id)
  render "your_template"
end

in you QR code generation, pass URL to above action with encrypted id like public_view_lot_url(lot_id)在您生成 QR 码时,将 URL 传递给上述操作,并使用public_view_lot_url(lot_id)等加密 id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM