我正在尝试用refraction实现入门ruby on rails的博客,当我想写一条评论时,它会说:
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
我的comments_controller代码是:
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
我希望你尽快回答感谢所有
最好的方法是将其定义为类似的私有方法
@comment = @post.comments.create(comment_params)
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end