Question

I am trying to implement the blog of the getting started of ruby on rails with refinery and when I want to write a comment it say that:

NoMethodError in CommentsController#create

undefined method `permit' for {"commenter"=>"lkjlkm", "body"=>",knm,m"}:ActiveSupport::HashWithIndifferentAccess
Rails.root: C:/Sites/blog2

Application Trace | Framework Trace | Full Trace
app/controllers/comments_controller.rb:8:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"rh9Kq6QxfXuZxlM9nFvsAjxiA5UnueEFKJ3ypcKy3xQ=",
 "comment"=>{"commenter"=>"lkjlkm",
 "body"=>",
knm,
m"},
 "commit"=>"Create Comment",
 "post_id"=>"5"}
Show session dump

Show env dump

Response

Headers:

None

my comments_controller code is that:

 class CommentsController < ApplicationController

http_basic_authenticate_with name: "antonio", password: "opening", only: :destroy

def create
@post = Post.find(params[:post_id])

@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
redirect_to post_path(@post)
end

def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end

end

I hope your answers soon thanks for all

Was it helpful?

Solution

The best way is to define it as a private method like this

@comment = @post.comments.create(comment_params)

private

def comment_params

   params.require(:comment).permit(:commenter, :body)

end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top