【问题标题】:passing # symbol to params hash in rails将#符号传递给rails中的参数散列
【发布时间】:2023-03-10 07:51:01
【问题描述】:

我在 Rails 应用程序中处理一些主题标签。

假设我已经编写了一个端点,它通过params 接收包含一些主题标签的字符串。有些是这样的

This string contains this #hashtag and this #one.

我正在使用邮递员来测试我的端点。

现在,当我在邮递员时,我会这样做:

{{url}}/add?content=content=This string contains this #hashtag and this #one

我在params 中收到字符串"content"=>"This string contains this "。也就是说,从符号# 的第一次出现开始,剩余的字符串被剪切。

很清楚我的问题有些幼稚,我敢问为什么?我做错了什么?以及如何管理传递包含# 符号的字符串?

【问题讨论】:

  • 很好的例子说明了为什么使用字符串连接或插值而不是使用适当的 URI 构建器来生成 URL 容易出错。
  • 您的请求中有两次content=

标签: ruby-on-rails ruby postman hashtag


【解决方案1】:

您需要确保you encode 在参数中发送任何内容。

URI.encode("This string contains this #hashtag and this #one")

#=> This%20string%20contains%20this%20%23hashtag%20and%20this%20%23one

【讨论】:

  • 怀着感激之情,我赞成并接受您的回答。问候
【解决方案2】:

更新您的邮递员请求,将# 替换为%23

{{url}}/add?content=This string contains this %23hashtag and this %23one

以后在做这样的事情时,你可以选择参数的一部分(或整个东西)并右键单击并选择EncodeURIComponent,如here所解释的那样

【讨论】:

    猜你喜欢
    • 2017-08-10
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 1970-01-01
    • 2011-09-19
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多