1

我的 ruby​​ 代码中有一个以反斜杠结尾的字符串:

acc.secret_key = "ASJSJJDSJFJJFFJJFJF\"
acc.save

以上是代码片段,当我尝试使用活动记录保存它时出现错误,我尝试添加另一个斜杠

acc.secret_key = "ASJSJJDSJFJJFFJJFJF\\"
acc.save

但现在我在数据库中有两个斜线。我错过了什么?多谢。

4

2 回答 2

2

您在控制台中看到了吗?如果是这种情况,您只是看到转义而不是两个实际的反斜杠。

string = "1234\\" 
# => "1234\\"
string.length 
# => 5 (if there were two \\'s the length would be 6)
string 
# => "1234\\"
puts string
# 1234\
# => nil

如果您在数据库控制台中查找带有转义反斜杠的记录,您应该会看到一个反斜杠。

tests_development=> select * from tests WHERE tests.id = 1;
 id |      name       |         created_at         |         updated_at         | public 
----+-----------------+----------------------------+----------------------------+--------
  1 | this is a test\ | 2013-02-05 21:44:12.339854 | 2013-02-05 21:44:12.339854 | t
(1 row)
于 2013-02-05T21:58:50.047 回答
0
acc.secret_key = "ASJSJJDSJFJJFFJJFJF//".to_string
acc.save

这会将带有反斜杠的字符串推送到数据库中

于 2013-02-05T18:15:07.350 回答