如何通过Rails API接受JSON而不使用嵌套字段的"_attributes"

Bri*_*ich 20 json ruby-on-rails

我用Rails编写了一个API,需要在API调用中接受一些nested_attributes.

目前我通过发送数据

PATCH /api/v1/vintages/1.json

{
  "vintage": {
    "year": 2014,
    "name": "Some name",
    "foo": "bar",
    "sizes_attributes": [
      {
        "name": "large",
        "quantity": "12"
      },
      {
        "name": "medium",
        "quantity": "2"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

但是,我想执行以下操作:

PATCH /api/v1/vintages/1.json

{
  "vintage": {
    "year": 2014,
    "name": "Some name",
    "foo": "bar",
    "sizes": [
      {
        "name": "large",
        "quantity": "12"
      },
      {
        "name": "medium",
        "quantity": "2"
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

区别在于属性是字段键的一部分.我希望能够accept_nested_attributes_for :sizes不必使用_attributes成为JSON对象的一部分.

谁知道如何管理这个?

ptd*_*ptd 28

您可以自由地在强参数方法中执行一些魔术.根据您的需要,您可能在控制器中使用此方法:

def vintage_params
  params.require(:vintage).permit(:year, :name, :foo, { sizes: [:name, :quantity] })
end
Run Code Online (Sandbox Code Playgroud)

您需要做的就是调整该sizes方法中键的名称.我建议:

def vintage_params
  vintage_params = params.require(:vintage).permit(:year, :name, :foo, { sizes: [:name, :quantity] })
  vintage_params[:sizes_attributes] = vintage_params.delete :sizes
  vintage_params.permit!
end
Run Code Online (Sandbox Code Playgroud)

这将删除:sizes密钥并将其置于预期中:sizes_attributes而不会弄乱你的漂亮的json.您无法直接accepts_nested_attributes_for更改名称.

  • "您无法直接使用accepts_nested_attributes_for来更改名称." 这是正确和不幸的.我有同样的问题. (3认同)

Tim*_*ore 7

我也在寻找一种方法来避免使用嵌套属性cruft污染我的RESTful API.我以为我会分享我的解决方案,因为它足以对遇到同样问题的人有用.它首先从您的控制器中利用一个简单的模块:

module PrettyApi
  class << self
    def with_nested_attributes(params, attrs)
      return if params.blank?

      case attrs
      when Hash
        with_nested_hash_attributes(params, attrs)
      when Array
        with_nested_array_attributes(params, attrs)
      when String, Symbol
        unless params[attrs].blank?
          params["#{attrs}_attributes"] = params.delete attrs
        end
      end
      params
    end

    private

    def with_nested_hash_attributes(params, attrs)
      attrs.each do |k, v|
        with_nested_attributes params[k], v
        with_nested_attributes params, k
      end
    end

    def with_nested_array_attributes(params, attrs)
      params.each do |np|
        attrs.each do |v|
          with_nested_attributes np, v
        end
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

以下是在控制器中使用此模块的示例,用于从移动客户端上载通讯簿:

class V1::AddressBooksController < V1::BaseController
  def create
    @address_book = AddressBook.new address_book_params
    unless @address_book.save
      errors = @address_book.errors.to_hash(true)
      render status: 422, json: { errors: errors }
    end
  end

  private

  def address_book_params
    PrettyApi.with_nested_attributes  pretty_address_book_params,
                                      contacts: [:emails, :phones, :addresses]
  end

  def pretty_address_book_params
    params.permit(
      :device_install_id,
      contacts: [
        :local_id,
        :first_name,
        :last_name,
        :nickname,
        emails: [
          :value,
          :type
        ],
        phones: [
          :value,
          :type
        ],
        addresses: [
          :type,
          :street_address,
          :city,
          :state,
          :postal_code,
          :country
        ]
      ]
    )
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意,声明嵌套属性的语法反映了在控制器中声明允许的参数的语法.

这是这个例子的要点.

我希望有人觉得这很有用!

  • @Branksy 在发布上述解决方案五年后,我认为嵌套属性不应该用于 REST/GraphQL API。因此,我不认为这是框架的错误,而是像我这样的开发人员滥用此功能的错误。如今,我提倡发送清晰描述数据的 JSON,并使用后端的服务对象来确定存储数据的位置和方式。 (3认同)