我的ruby代码中有一个字符串,以反斜杠结尾:
acc.secret_key = "ASJSJJDSJFJJFFJJFJF\"
acc.save
上面是代码片段,当我尝试使用Active记录保存它时出现错误,我尝试添加另一个斜杠
acc.secret_key = "ASJSJJDSJFJJFFJJFJF\\"
acc.save
但现在我在DB中有两个斜线。我错过了什么?感谢很多。
答案 0 :(得分:2)
你在控制台看到这个吗?如果是这种情况,你只是看到逃避的不是两个实际的反斜杠。
string = "1234\\"
# => "1234\\"
string.length
# => 5 (if there were two \\'s the length would be 6)
string
# => "1234\\"
puts string
# 1234\
# => nil
如果在db控制台中查找带有反斜杠的记录,则应该看到一个反斜杠。
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)
答案 1 :(得分:0)
acc.secret_key = "ASJSJJDSJFJJFFJJFJF//".to_string
acc.save
这会将带有backshash的字符串推送到数据库