尝试通过 curl post 评论我的 post 但它不起作用
Trying to post comment to my post via curl and it doesn't work
我有路线
POST /posts/:post_id/comments(.:format) comments#create
我的评论有 "content" 字段。假设我想 POST 对我的 post.
发表评论
所以我正在尝试 post 通过 curl 执行此命令发表评论
curl -X POST -H "Content-Type: application/json" -d '{"comment": "hello"}' http://localhost:3000/posts/2/comments
但我得到了一个动作控制器:捕获到异常
不知道为什么
控制器
# POST /comments
# POST /comments.json
def create
@comment = @post.comments.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:comment)
end
编辑:
error in log file
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `permit' for "hello":String):
app/controllers/comments_controller.rb:89:in `comment_params'
app/controllers/comments_controller.rb:33:in `create'
编辑:
curl -X POST -H "Content-Type: application/json" -d '{"comment":{"comment":"test"}}' http://localhost:3000/posts/8/comments
为我工作,但我有点困惑为什么当我为我的 POST 做类似的卷曲时:
curl -H "Content-Type: application/json" -d '{"title":"funny title","content":"cool stuff", "user_id":2}' localhost:3000/posts
和我的 post 参数看起来像这样 params.require(:post).permit(:title, :content, :user_id)
为什么不在 curl 哈希中传递 post 就可以工作?
你说的是"for the comment object, permit the comment attribute"
params.require(:comment).permit(:comment)
所以 rails 期待这样的事情:
{"comment": {"comment": "hello"}}
如果你想快速修复它 -> 你需要使用上面的数据。
否则你需要弄清楚真正的评论表单的数据是什么样子,并更新你的 require/permit 行以匹配
我有路线
POST /posts/:post_id/comments(.:format) comments#create
我的评论有 "content" 字段。假设我想 POST 对我的 post.
发表评论所以我正在尝试 post 通过 curl 执行此命令发表评论 curl -X POST -H "Content-Type: application/json" -d '{"comment": "hello"}' http://localhost:3000/posts/2/comments
但我得到了一个动作控制器:捕获到异常
不知道为什么
控制器
# POST /comments
# POST /comments.json
def create
@comment = @post.comments.new(comment_params)
respond_to do |format|
if @comment.save
format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
format.json { render :show, status: :created, location: @comment }
else
format.html { render :new }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:comment)
end
编辑:
error in log file
Completed 500 Internal Server Error in 2ms
NoMethodError (undefined method `permit' for "hello":String):
app/controllers/comments_controller.rb:89:in `comment_params'
app/controllers/comments_controller.rb:33:in `create'
编辑: curl -X POST -H "Content-Type: application/json" -d '{"comment":{"comment":"test"}}' http://localhost:3000/posts/8/comments
为我工作,但我有点困惑为什么当我为我的 POST 做类似的卷曲时:
curl -H "Content-Type: application/json" -d '{"title":"funny title","content":"cool stuff", "user_id":2}' localhost:3000/posts
和我的 post 参数看起来像这样 params.require(:post).permit(:title, :content, :user_id)
为什么不在 curl 哈希中传递 post 就可以工作?
你说的是"for the comment object, permit the comment attribute"
params.require(:comment).permit(:comment)
所以 rails 期待这样的事情:
{"comment": {"comment": "hello"}}
如果你想快速修复它 -> 你需要使用上面的数据。 否则你需要弄清楚真正的评论表单的数据是什么样子,并更新你的 require/permit 行以匹配