Ruby on rails 向链接添加图像的Ruby字符串插值在图像src前面加一个斜杠
我正在使用Ruby 2.3.1p112,并尝试使用字符串插值生成图像链接。但是,它错误地转义了链接src引号,如下所示:src=\“\”。示例如下所示:Ruby on rails 向链接添加图像的Ruby字符串插值在图像src前面加一个斜杠,ruby-on-rails,ruby,string-interpolation,ruby-2.2,Ruby On Rails,Ruby,String Interpolation,Ruby 2.2,我正在使用Ruby 2.3.1p112,并尝试使用字符串插值生成图像链接。但是,它错误地转义了链接src引号,如下所示:src=\“\”。示例如下所示: <a href=www.google.com target='_blank'> google.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a> 如果运行上面的代码,您将看到puts语句记录了正确的内容,图像src是
<a href=www.google.com target='_blank'> google.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>
如果运行上面的代码,您将看到puts语句记录了正确的内容,图像src是:
<a href=http://www.facebook.com>facebook.com <img src="http://localhost:3000/t" width='1' height='1'> </a>
但如果没有puts语句,图像src将被转义引号包围:http://localhost:3000/t\"
删除图像src中引号转义的最佳方法是什么?没有反斜杠。您的代码工作正常 您可以通过在irb中运行下面的代码来复制它 尝试在
irb
中运行此命令:
puts '"hello"'
# => "hello"
'"hello"'
# => "\"hello\""
您所看到的是,当直接输出变量时,irb
显示原始字符串。由于字符串以“
字符结尾,因此有必要在显示时转义输出中的任何”
字符
如果字符串确实包含文字反斜杠,您会看到什么而不是
<a href=http://www.facebook.com target='_blank'> facebook.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>
将是:
<a href=http://www.facebook.com target='_blank'> facebook.com \\\"<img src=\\\"http://localhost:3000/t\\\" width='1' height='1'>\\\"</a>
没有反斜杠。您的代码运行正常 您可以通过在irb中运行下面的代码来复制它 尝试在
irb
中运行此命令:
puts '"hello"'
# => "hello"
'"hello"'
# => "\"hello\""
您所看到的是,当直接输出变量时,irb
显示原始字符串。由于字符串以“
字符结尾,因此有必要在显示时转义输出中的任何”
字符
如果字符串确实包含文字反斜杠,您会看到什么而不是
<a href=http://www.facebook.com target='_blank'> facebook.com \"<img src=\"http://localhost:3000/t\" width='1' height='1'>\"</a>
将是:
<a href=http://www.facebook.com target='_blank'> facebook.com \\\"<img src=\\\"http://localhost:3000/t\\\" width='1' height='1'>\\\"</a>
用“斜杠前缀”表示“反斜杠后缀”?用“斜杠前缀”表示“反斜杠后缀”?