2

我有两种类型的 Rails 对象帖子和评论。假设每个帖子has_many评论,每个评论belongs_to一个帖子。

当我构建我的评论 JSON 字符串时,我会这样做:

{"id":0,"title":"something","body":"something","post_id":1}

但我回来了

{"id":0,"title":"something","body":"something","post_id":NULL}

我将 POST json 请求发送到:/comments.json因为在这种情况下 Post 对象是一个单例。

这是我的路线:

  resources :posts
  resources :comments

有人知道我可以在这里解决什么吗?

4

1 回答 1

2

我的猜测是你不能massasign了post_id

post_id您可以通过将此行添加到您的模型来允许仅针对 的质量分配:

attr_accessible :post_id

在这里查看更多信息。

于 2012-04-10T02:34:54.180 回答