0

(はい、試しましたautosave: true)

ユーザー、投稿、コメントの 3 つのモデルがあります。コメントは投稿とユーザーに属します。

したがって、Comments#create には次の行があります。

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

post_id は params で として渡され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 に答える