在我的评论控制器中,我有以下内容
类 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>
为什么这里有这么多东西是零?当代码正常工作时,我对该方法的所有参数build_from
都与以前相同。
尝试调用时的终端输出@comment.save
是
undefined method 'user' for #<Comment:0x5d1b1b0>
我不确定这意味着什么。我有一个 user_id 关联。我的参数哈希中没有看到用户参数。
任何帮助将非常感激。