簡體   English   中英

Rails允許嵌套數組

[英]Rails permit nested array

我正在嘗試將hass_many關聯與accepts_nested_attributes_for結合使用,並且遇到很多麻煩...

這是我的user.rb的簡化版本:

class User < ActiveRecord::Base
  ...
  has_many :user_permissions
  accepts_nested_attributes_for :user_permissions
  ...
end

我的user_permission.rb

class UserPermission < ActiveRecord::Base
  belongs_to :user
  ...
end

和我的users_controller.rb

class UsersController < ApiController
  ...    
  def update
    @user.assign_attributes user_params
    if @user.save
      render partial: 'user', locals: { user: @user }
    else
      render json: {errors: @user.errors}.to_json, status: 500
    end
  end  
  ...
private
  def user_params
    params.require(:user).permit(:first_name, :last_name, user_permissions_attributes: [ :user_id, :resource_id, :can_read, :can_update, :can_create, :can_delete ])
  end
end

我正在參考該Rails文檔 ,了解如何將Strongs Parameters與accepts_nested_attributes_for一起使用。

但是,當我從users_controller內部放入user_params時 ,這就是我所看到的(沒有引用user_permissions):

{"first_name"=>"Joe", "last_name"=>"Shmoe"}

這是我要提交給服務器的JSON示例(通過angular $ resource):

{
  "id": 10,
  "first_name": "Joe",
  "last_name": "Shmoe",
  "user_permissions": [
    {
      "organization_resource_id": 20,
      "user_id": 10,
      "can_update": true,
      "can_read": true
    },
    {
      "organization_resource_id": 21,
      "user_id": 10,
      "can_create": true,
      "can_read": true
    }
  ],
}

它返回以下JSON:

{
  "id": 10,
  "first_name": "Joe",
  "last_name": "Shmoe",
  "user_permissions": [],
}

我相當有信心這是我的rails層中的一個問題,但是僅供參考,這是我創建的用於與服務器執行這種RESTful交互的有角度的User.js服務:

angular.module('slics').service('User', [
  '$resource', function($resource) {
    return $resource('/api/users/:id', {
      id: '@id'
    }, {
      update: {
        method: 'PUT',
        isArray: false
      }
    });
  }
]);

真的不確定我在這里想念的是什么。 提交嵌套屬性似乎並不那么困難……但是我做的研究越多,我就越意識到這似乎是一種常見的Rails挫敗感。

如果在我的問題描述中包括任何其他上下文/信息將很有用,以幫助解決此問題,請隨時發表評論,我很樂意提供!

那么,你發布哈希的數組,而不是一個哈希值。

所以這段代碼

user_permissions_attributes: [ :user_id, :resource_id, :can_read, :can_update, :can_create, :can_delete ]

將允許這樣的結構

 {
  "id": 10,
  "first_name": "Joe",
  "last_name": "Shmoe",
  "user_permissions_attributes": [    
      "organization_resource_id": 20,
      "user_id": 10,
      "can_update": true,
      "can_read": true
  ]
}

嘗試將所有參數列入“ user_permissions”白名單

user_permissions_attributes: []

或查看這篇文章,了解如何使用StrongParams建立高級白名單http://patshaughnessy.net/2014/6/16/a-rule-of-thumb-for-strong-parameters

強大的參數需要user_permissions_attributes ,並且您正在提交user_permissions

強參數與accepts_nested_attributes_for是分開的(實際上,它與它無關),因此可以定義require! / permit調用正是應提交屬性的方式。

ProTip:為了節省以后的麻煩 ,如果您打算通過接受嵌套屬性進行更新,則可能還希望允許:id

user_permissions_attributes:[:user_id,:id,:can_read,:can_update,:can_create,:can_delete])允許:id並提交具有索引值的哈希。

JSON格式提交到服務

"user": {
   "id": 10,
   "first_name": "Joe",
   "last_name": "Shmoe",
   "user_permissions": {
     "0": {
       "id": 20,
       "user_id": 10,
       "can_update": true,
       "can_read": true
      },
      "1": {
        "id": 21,
        "user_id": 10,
        "can_create": true,
        "can_read": true
      }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM