【问题标题】:Rails permit nested attributeRails 允许嵌套属性
【发布时间】:2021-10-12 00:02:59
【问题描述】:

我正在使用 ruby​​-2.6.5 开发 Rails 6,并且正在开发 API。我正在为我的订单使用嵌套属性,如下所示:-

orders_controller.rb
# frozen_string_literal: true

module Api
  module V1
    class OrdersController < Api::V1::ApiApplicationController
      before_action :validate_token

      def create
        debugger
        order = OrderInteractor.create(order_params, @user_id)
        if order.save
          render json: { 'message' => 'Order Placed' }, status: :ok
        else
          render_errors(order)
        end
      end

      private

      def order_params
        params.require(:data)
              .require(:attributes)
              .require(:order)
              .permit(:user_id, :address_id, :total_price, :payment_status,
                      :order_number, :delivery_time_slot,
                      order_details_attributes:
                      %i[price quantity order_detail_status product_id
                         order_number variant_id],
                      payment_details_attributes:
                      %i[payment_data payment_id])
      end
    end
  end
end

API 请求:-

{
    "data": {
        "attributes": {
            "order": {
                "address_id": "82",
                "delivery_time_slot": "5:00 PM - 8:00 PM(Today)",
                "order_details_attributes": [{
                    "price": "76.0",
                    "product_id": "46",
                    "quantity": "4",
                    "variant_id": "47"
                }, {
                    "price": "9.9",
                    "product_id": "30",
                    "quantity": "1",
                    "variant_id": "29"
                }],
                "payment_details_attributes": [{
                    "payment_data": {
                        "data": {
                            "nameValuePairs": {
                                "razorpay_payment_id": "pay_HiHceX2p6450Wa",
                                "org_logo": "",
                                "org_name": "Razorpay Software Private Ltd",
                                "checkout_logo": "https://cdn.razorpay.com/logo.png",
                                "custom_branding": false
                            }
                        },
                        "paymentId": "pay_HiHceX2p6450Wa",
                        "userContact": "+916494949494",
                        "userEmail": "dailyferia@gmail.com"
                    }
                }],
                "total_price": "354"
            }
        },
        "type": "orders"
    }
}

在下订单时,我收到错误Unpermitted parameter: :payment_data,但它对于 order_details 工作正常。请帮我解决它?我也尝试了以下方法来修复它,但没有任何效果:-

payment_details_attributes: %i[:payment_data payment_id]) and `payment_details_attributes: ['payment_data', 'payment_id'])`

【问题讨论】:

    标签: ruby-on-rails strong-parameters


    【解决方案1】:

    您的 payment_data 是一个复杂对象,而不是在您的 order_details_attributes 中找到的标量

    您需要在允许的参数中添加更多,我相信最简单的解决方案是:

    payment_details_attributes: [payment_data: {}]
    

    这应该接受payment_details_attributes 下的所有参数,但它也允许任何其他键。你可能想要更严格,只允许上面指定的参数,在这种情况下你可以这样做:

    payment_details_attributes: [
      payment_data: {
        data: {
          nameValuePairs:
          %i[razorpay_payment_id org_logo org_name checkout_logo custom_branding]
        },
        :paymentId, :userContact, :userEmail
      }
    ]
    

    这应该将参数限制为您的示例中使用的格式。

    其他几点说明:

    • 您的原始样本中有%i[payment_data payment_id],但您的有效负载中没有payment_id。示例中的属性是paymentId,除此之外,它是payment_data 的属性,而不是payment_details_attributes
    • 您不会使用%i 和冒号,%i 是创建红宝石符号数组的简写,因此%i[:payment_data payment_id] 将创建数组[:":payment_data", :payment_id](注意开头的额外冒号付款数据)

    最后,我还没有测试我上面的代码,所以可能存在语法或其他错误,但希望这会为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多