0

(是的,我试过了autosave: true

我有三个模型:用户、帖子、评论。评论属于帖子和用户。

所以在 Comments#create 我有以下行:

@comment = current_user.comments.build(params[:comment])

post_id 在参数中作为comment: {post_id: post_id}. 但是,mongoid 在保存对象时完全忽略它。显然我可以通过添加类似的东西来解决它@comment.post = post。这是一个正确的解决方案还是我可以做得更好更清洁?

4

1 回答 1

0

似乎用 params 传递的那个是 JSON 格式的?然后它是散列而不是对象格式。

可能,json不能直接转换。

尝试这个

comment=Comment.new(:post_id => params[:comment][:post_id])
#try to replace symbol [:post_id] with string ["post_id"] if it did not work
current_user.comments.build(comment)
于 2013-08-25T13:56:24.750 回答