私のコメントコントローラーには、次のものがあります
class CommentsController < ApplicationController
def create
@comment_hash = params[:comment]
#@comment = comment.new(comment_params)
@obj = @comment_hash[:commentable_type].constantize.find(@comment_hash[:commentable_id])
@comment = Comment.build_from(@obj, current_user.id, @comment_hash[:body])
binding.pry
#...
ある地点でbinding.pry
は、
@comments_hash は
{"body=>"asdf comment body",
"commentable_type"=>"Hack",
"commentable_id"=>"2"}
@objは
#<Hack id: 2, created_at: "2014-09-16 00:00:00",
updated_at: "2014-09-16 00:00:00",
body: "some text",
user_id: 1,
comment_threads_count: 0>
しかし @comment 出力
#<Comment id: nil,
commentable_id: 2,
commentable_type: "Hack",
title: nil, body: "asdf,
comment body",
subject: nil,
user_id: 1,
parent_id: nil,
lfg: nil, rgt:
nil, created_at: nil,
updated_at: nil>
ここでは、なぜ多くのものが nil なのですか? メソッドのすべての引数はbuild_from
、コードが正しく機能していたときと同じです。
電話をかけようとしたときの端末出力は次のとおり@comment.save
です。
undefined method 'user' for #<Comment:0x5d1b1b0>
これが何を意味するのかわかりません。user_id 関連付けがあります。params ハッシュにユーザー パラメータがありません。
どんな助けでも大歓迎です。