【问题标题】:Rails permit nested arrayRails 允许嵌套数组
【发布时间】:2015-08-02 01:37:27
【问题描述】:

我正在尝试将 accepts_nested_attributes_forhas_many 关联结合使用,但遇到了很多麻烦...

这是我的 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

我正在参考this rails documentation,了解如何使用带有强参数的accepts_nested_attributes_for。

但是,当我从 users_controller 内部“puts 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 层中的一个问题,但这里仅供参考的是我创建的 angular User.js 服务,用于与服务器执行这种 RESTful 交互:

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

真的不确定我在这里缺少什么。提交嵌套属性似乎不应该这么难......但是我做的研究越多,我就越意识到这似乎是一个很常见的 Rails 挫折。

如果在我的问题描述中包含任何其他上下文/信息有助于解决此问题,请随时发表评论,我很乐意提供!

【问题讨论】:

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


    【解决方案1】:

    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
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      好吧,你发布了一个 散列 数组,而不是散列。

      所以这段代码

      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

      【讨论】:

      • permit 调用实际上不允许您作为示例显示的 JSON 有效负载。将允许user_permissions_attributes,但不允许user_permissions
      【解决方案3】:

      强参数需要user_permissions_attributes,而您提交的是user_permissions

      强参数与accepts_nested_attributes_for 是分开的(实际上,它与它无关),所以无论你定义你的require!/permit 调用,你的属性应该如何提交。

      专业提示:为了避免您日后感到沮丧,如果您打算通过接受嵌套属性进行更新,您可能也希望允许 :id

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多