有一段代码好像不工作了,大家帮看看
def likeable
@topic = Topic.last
## likeable 是一个字符串 类似 "127.0.0.1 127.0.0.2 "
@topicarr = @topic.likeable.split
## 就是这段代码有问题 ip 无法插入到数据库字段里
@topic.likeable << request.remote_ip.to_s + ' '
render :text => @topic.likeable.split.length if @topic.save
end
end
rails server 中的 log
Started GET "/topics/likeable" for 127.0.0.1 at 2012-11-23 13:33:45 +0800
Processing by TopicsController#likeable as HTML
Topic Load (5.7ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
(0.8ms) BEGIN
(3.5ms) COMMIT
Rendered text template (0.0ms)
Completed 200 OK in 14ms (Views: 0.7ms | ActiveRecord: 10.0ms)
看 log 输出好像没有保存的过程
1.9.3p125 :042 > Topic.last.likeable
=> ""
大家帮忙看看
render :text => @topic.likeable.split.length if @topic.save 改成 if @topic.save render :text => @topic.likeable.split.length else render :text => @topic.errors end 看看
#1 楼 @ywencn 没有输出错误 数据库还是没插入成功 不过 log 有一条错误
Started GET "/topics/likeable" for 127.0.0.1 at 2012-11-23 14:07:52 +0800
Processing by TopicsController#likeable as HTML
Topic Load (3.2ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
(2.6ms) BEGIN
(1.4ms) COMMIT
Rendered text template (0.0ms)
Completed 200 OK in 11ms (Views: 0.7ms | ActiveRecord: 7.2ms)
[2012-11-23 14:07:52] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
1.9.3p125 :016 > t = Topic.last
Topic Load (6.5ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
=> #<Topic id: 1, body: "<span style=\"color:#333333;font-size:14px;backgroun...", likeable: "127.0.0.1 127.0.0.1 ", unlikeable: "", timeint: 201211237, created_at: "2012-11-23 05:02:23", updated_at: "2012-11-23 05:24:57">
1.9.3p125 :017 > t.likeable << "127.0.0.2" + " "
=> "127.0.0.1 127.0.0.1 127.0.0.2 "
1.9.3p125 :018 > t.likeable
=> "127.0.0.1 127.0.0.1 127.0.0.2 "
1.9.3p125 :019 > t.save
(0.2ms) BEGIN
(0.3ms) COMMIT
=> true
1.9.3p125 :020 > Topic.last.likeable
Topic Load (15.8ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
=> "127.0.0.1 127.0.0.1 "
1.9.3p125 :021 >
在 console 里面也是无法保存成功??
1.9.3p125 :016 > t = Topic.last
Topic Load (6.5ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
=> #<Topic id: 1, body: "<span style=\"color:#333333;font-size:14px;backgroun...", likeable: "127.0.0.1 127.0.0.1 ", unlikeable: "", timeint: 201211237, created_at: "2012-11-23 05:02:23", updated_at: "2012-11-23 05:24:57">
1.9.3p125 :017 > t.likeable << "127.0.0.2" + " "
=> "127.0.0.1 127.0.0.1 127.0.0.2 "
1.9.3p125 :018 > t.likeable
=> "127.0.0.1 127.0.0.1 127.0.0.2 "
1.9.3p125 :019 > t.save
(0.2ms) BEGIN
(0.3ms) COMMIT
=> true
1.9.3p125 :020 > Topic.last.likeable
Topic Load (15.8ms) SELECT `topics`.* FROM `topics` ORDER BY `topics`.`id` DESC LIMIT 1
=> "127.0.0.1 127.0.0.1 "
1.9.3p125 :021 >
在 console 里面也是无法保存成功??
#7 楼 @woaigithub
@topic.likeable << request.remote_ip.to_s + ' '
这一行代码 就是更新字段
#1 楼 @ywencn #6 楼 @woaigithub #9 楼 @cantin 问题解决了,虽然不知道问题出在哪里了 解决方法 开始时 likeable 字段是 string 后来换成了 text 代码也跟着改了
def likeable
@topic = Topic.last
@topicarr = @topic.likeable.to_s.split
@topic.likeable = @topic.likeable.to_s + request.remote_ip.to_s + ' '
if @topic.save
render :text => @topic.likeable.split.length
else
render :text => @topic.errors
end
end
感觉没有问题啊,你试试 不用“<<”而采用 t.likeable = t.likeable+t.likeable+request.remote_ip.to_s
,然后再 t.save
#15 楼 @dotnil
这种 in-place 的写法是会导致这个问题的,Rails 上已经有注释说明了
https://github.com/rails/rails/blob/master/activemodel/lib/active_model/dirty.rb#L81-88