Rails 4强参数与JSON数组 [英] Rails 4 Strong parameters with JSON array

查看:152
本文介绍了Rails 4强参数与JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布JSON对象数组,但是我始终会收到422无法处理的实体响应.
这是我的控制器操作:

I am trying to post an array of JSON objects, but i always get a 422 Unprocessable Entity response.
This is my controller action:

def create
 @myusers = params[:user]
 @myusers.each do
 User.create(user_params)
end     

def user_params
             params.permit(:name,:address )
end

连同下面显示的http调用,@myuser数组看起来像这样

Where,together with the below shown http call, the @myuser array looks like this

@myuser =[{"name"=>"name11", "address"=>"addr1"}, {"name"=>"name22", "address"=>"addr2"}, {"name"=>"name33", "address"=>"addr132"}, {"name"=>"name4", "address"=>"addr4"}]

这是HTTP JSON正文

This is the HTTP JSON body

{"user":[{
   "name" : "name11",
   "address":"addr1"
},
{
    "name" : "name22",
    "address":"addr2"
},
{
    "name" : "name33",
    "address":"addr132"
},
{
    "name" : "name4",
    "address":"addr4"
}
]}

,我添加了Accept/Content-Type : application/json标头. 日志文件显示了此

and i added the Accept/Content-Type : application/json header. The log file shows this

Started POST "/api/customer/" for 127.0.0.1 at 2015-11-02 18:45:09 +0100
Processing by Api::UserController#create as JSON
Parameters: {"user"=>[{"name"=>"name11", "address"=>"addr1"},  {"name"=>"name22", "address"=>"addr2"}, {"name"=>"name33", "address"=>"addr132"}, {"name"=>"name4", "address"=>"addr4"}]}
Unpermitted parameters: customer, format
   (0.4ms)  BEGIN
   (0.3ms)  ROLLBACK
Unpermitted parameters: customer, format
   (0.3ms)  BEGIN
   (0.3ms)  ROLLBACK
Unpermitted parameters: customer, format
   (0.2ms)  BEGIN
   (0.6ms)  ROLLBACK
Unpermitted parameters: customer, format
   (0.3ms)  BEGIN
   (0.4ms)  ROLLBACK
Unpermitted parameters: customer, format
   (0.3ms)  BEGIN
   (0.4ms)  ROLLBACK
Completed 422 Unprocessable Entity in 62ms (Views: 0.6ms | ActiveRecord: 7.0ms)

每个单个对象都是一个ApplicationControllerParameter,因此应该可以为每个条目直接调用user_params动作吗?还是我错了?

Each of the single objects is an ApplicationControllerParameter so it should be possible to call directly the user_params action for each entry? Or am i wrong there?

在此先感谢您的帮助.

解决方案:

我认为该代码有可能可以做得更好一些,但是现在对我有用:

I think there s some possibility that this code can be done a bit more nice but this is now working for me:

  def user_params
         params.permit(user: [:name,:address])
  end

  def create 
         user_params[:user].each do |u|
             User.create(u)
         end
  end

推荐答案

关于参数的问题,您遇到的问题是您正在使用整个params哈希,而不是将其范围缩小到"user"位,即您有兴趣允许.因此,您获得了所有已通过的其他信息,例如格式. (顺便说一句,难道不应该更正确地称其为用户"而不是单数的用户"吗?无论如何……不要介意:)

Your issue as far as the question about parameters is concerned is that you're using the whole params hash and not narrowing it down to the 'user' bit that you're interested in permitting. So you're getting all the other stuff that's passed e.g. format. (Incidentally, shouldn't it more correctly be called 'users' rather than the singular 'user'? Anyway... Never mind that :)

我认为应该是

params.permit(user: [:name, :address])

params.require(:user).permit?([:name, :address])

strong_parameters gem的文档中有一些关于嵌套参数的信息:

There's a bit about nested parameters in the docs of the strong_parameters gem here:

https://github.com/rails/strong_parameters#nested-parameters

希望有帮助...

这篇关于Rails 4强参数与JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆