将 # 符号传递给 rails 中的参数散列
passing # symbol to params hash in rails
我正在 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 "
。也就是说,从第一次出现符号 #
开始,剩余的字符串将被剪切。
很清楚我的问题有些幼稚,我敢问为什么?我做错了什么?以及如何管理传递包含 #
符号的字符串?
您需要确保 you encode 无论您在参数中发送什么。
URI.encode("This string contains this #hashtag and this #one")
#=> This%20string%20contains%20this%20%23hashtag%20and%20this%20%23one
更新您的邮递员请求,将 #
替换为 %23
{{url}}/add?content=This string contains this %23hashtag and this %23one
将来做这样的事情时,您可以 select 参数的一部分(或整个参数)并右键单击并 select EncodeURIComponent
,如解释的那样 here
我正在 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 "
。也就是说,从第一次出现符号 #
开始,剩余的字符串将被剪切。
很清楚我的问题有些幼稚,我敢问为什么?我做错了什么?以及如何管理传递包含 #
符号的字符串?
您需要确保 you encode 无论您在参数中发送什么。
URI.encode("This string contains this #hashtag and this #one")
#=> This%20string%20contains%20this%20%23hashtag%20and%20this%20%23one
更新您的邮递员请求,将 #
替换为 %23
{{url}}/add?content=This string contains this %23hashtag and this %23one
将来做这样的事情时,您可以 select 参数的一部分(或整个参数)并右键单击并 select EncodeURIComponent
,如解释的那样 here