我在form_for中有以下内容:
= form_for @activity do |f|
.field
= label :comment, :comment
= text_area :comment, :comment, :rows => 4
我没有调用f.label或f.text_area,因为comment是一个单独的模型,我将它单独保存。上面的代码工作正常,但是当验证在@activity上失败时,注释就会被删除。我希望注释字段在@activity验证失败时重新填充,所以我尝试了以下内容:
= form_for @activity do |f|
.field
= label :comment, :comment
= text_area :comment, :comment, :value => @comment, :rows => 4
在控制器操作中使用此行:
@comment = Comment.new(params[:comment][:comment])
然而,有了这个,我收到以下错误:
undefined method `stringify_keys' for "hello":String
我在上面显示的控制器中的@comment分配失败了。
这里发生了什么?
错误后的参数包含以下内容:
"comment"=>{"comment"=>"hello"}
答案 0 :(得分:3)
问题在于Comment#new
需要一个Hash,其键对应于Comment的属性,但是你给它params[:comment][:comment]
,其值是一个字符串(在本例中为"hello"
)。我怀疑这是你想要的:
@comment = Comment.new params[:comment]