Rails4强参数,如何允许关联数组?

时间:2014-08-30 07:49:09

标签: ruby-on-rails ruby-on-rails-4

视图(HAML):

- kind.apanages.each do |apanage|
  = number_field_tag "fact[apanage[#{apanage.id}]]", ''

控制器:

1。不要使用许可证:

render :json => params
  

{ “UTF8”: “✓”, “实”:{ “ENTITY_ID”: “27”, “属地”:{ “1”: “7”, “2”: “13”, “3”: “2”, “4”: “1”, “6”: “20”, “7”: “23”, “8”: “10”,}}, “提交”: “结束”, “动作” : “事实”, “控制器”: “实体”}

2。使用许可证(var 1):

render :json => params.require(:fact).permit(:entity_id, :apanage => [])
  

{ “ENTITY_ID”: “27”}

3。使用许可证(第2版):

render :json => params.require(:fact).permit(:entity_id, apanage: [:id, :content])
  

{ “ENTITY_ID”: “27”, “属地”:{}}

我如何允许阵列'apanage'?

1 个答案:

答案 0 :(得分:1)

尝试:

params.require(:fact).permit(:entity_id).permit!(:apanage)

另请参阅this discussion