如何将application / json POST请求映射到rails中的params?

时间:2022-03-09 22:03:10

I am being passed in a json body for a post request, but i'm not sure the best way to map this json body to request params in rails:

我正在json体内传递一个帖子请求,但我不确定映射这个json体的最佳方法是在rails中请求params:

{
   username: "example-user",
   password: "password",
   email: "example@gmail.com",
}

and in the controller i wish to acccess params["username"], is this possible? how should i do it?

并且在控制器中我希望使用params [“username”],这可能吗?我该怎么办?

1 个解决方案

#1


6  

see the guides for a comprehensive information.

请参阅指南以获取全面的信息。

you can simply access to params[:username] or even params['username'] since params is a HashWithIndifferentAccess.

你可以简单地访问params [:username]甚至params ['username'],因为params是一个HashWithIndifferentAccess。

Note that you can easily inspect the params using the debugger gem, or just slam a raise in your controller action to see them on the error page when in development mode.

请注意,您可以使用调试器gem轻松检查params,或者只是在控制器操作中猛烈加注,以便在开发模式下在错误页面上查看它们。

#1


6  

see the guides for a comprehensive information.

请参阅指南以获取全面的信息。

you can simply access to params[:username] or even params['username'] since params is a HashWithIndifferentAccess.

你可以简单地访问params [:username]甚至params ['username'],因为params是一个HashWithIndifferentAccess。

Note that you can easily inspect the params using the debugger gem, or just slam a raise in your controller action to see them on the error page when in development mode.

请注意,您可以使用调试器gem轻松检查params,或者只是在控制器操作中猛烈加注,以便在开发模式下在错误页面上查看它们。