I have the following within a form_for:
= form_for @activity do |f|
.field
= label :comment, :comment
= text_area :comment, :comment, :rows => 4
I am not calling f.label or f.text_area because comment is a separate model and I am saving it separately. The above code works fine, but when the validation fails on @activity, then the comment gets wiped out. I would like for the comment field to repopulate when the @activity validation fails, so I tried the following:
= form_for @activity do |f|
.field
= label :comment, :comment
= text_area :comment, :comment, :value => @comment, :rows => 4
With this line in the controller action:
@comment = Comment.new(params[:comment][:comment])
With this, however, I get the following error:
undefined method `stringify_keys' for "hello":String
It is failing on the @comment assignment in the controller which I show above.
What is going on here?
The params after the error contain the following:
"comment"=>{"comment"=>"hello"}
The problem is that
Comment#newexpects a Hash whose keys correspond to Comment’s attributes but you’re giving itparams[:comment][:comment], whose value is a string (in this case,"hello"). I suspect that this is what you want instead: