尝试使用 Postman 将请求发布到 localhost 服务器时出现参数错误

Argument Error when trying Post request to localhost server using Postman

提问人:M_Shkreli21 提问时间:3/21/2023 更新时间:3/21/2023 访问量:27

问:

我现在正在尝试为一个项目设置我的数据库,但遇到了阻止我继续前进的问题。我设置了下面的控制器来使用 Rails 创建新帖子。

def create
        new_post = Post.create!(post_params)
        render json: new_post, status: :created
    end

def post_params
        params.permit(:post_body, :user_id, :likes, :sports_id)
    end

我设置了初始种子数据,当我运行 db:seed 时,我没有收到任何错误,它显示了我在文件中设置的完整消息。当我在 Postman 中运行 GET 请求时,我得到一个空数组,显示种子数据从未通过。如果我尝试使用 Postman 执行 POST 请求,则会出现以下错误:

在此处输入图像描述

有谁知道发生了什么?

我尝试通过终端手动输入测试帖子,但仍然出现错误。我看过我以前做过的项目,这些项目具有类似的设置,例如帖子,但我不记得遇到过这个错误。

JSON Ruby-on-Rails 邮递员

评论


答:

0赞 LihnNguyen 3/21/2023 #1

@Hi M_Shkreli21,

要知道它是如何失败的,你应该使用调试来检查

def create
   new_post = Post.new(post_params)
   # put byebug or debugger
   if new_post.save
     render json: new_post, status: :created
   else
     render json: "#{new_post.errors.full_message}", status: unprocessable_entity
   end
end

之后,您使用 postman 请求

示例:我的主机enter image description here

您可以检查 , , ...paramspost_paramsnew_post.errors.full_message

=> Finaly,你可以修复它