Reputation: 153
while i am creating a comment associated with the posts, i am getting this error::
My comments controller ::
class CommentsController < ApplicationController
def new
@comments = Comment.new
end
def create
@post = Post.find (params[:post_id])
@comments = @post.comments.create(params[:comments].permit(:commenter, :body))
redirect_to post_path(@post)
end
end
// The form for the comments ///
<strong>Title:</strong>
<%= @post.Title %>
</p>
<p>
<strong>Text:</strong>
<%= @post.Text %>
</p>
<%= form_for([@post, @post.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br />
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br />
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
i am getting error on this line::
@comments = @post.comments.create(params[:comments].permit(:commenter, :body))
Please point me out where i am wrong..
One edit :: My actual error statement ::
NoMethodError in CommentsController#create
Upvotes: 1
Views: 3323
Reputation: 1299
The correct syntax to use strong parameter is
params.require(:comments).permit(:commenter, :body)
But I think params will contain comment
not comments
So you should use
params.require(:comment).permit(:commenter, :body)
Upvotes: 4
Reputation: 25039
well, as the error message states, params[:comments]
is nil.
You should be using params.require(:comments).permit(:commenter, :body)
so that if comments
isn't present, it won't go any further.
Also, the actual param being submitted is comment
, not comments
. You can verify this by looking at the submitted params in your logs.
Upvotes: 2