具有不同参数结构的多个动作

时间:2018-06-19 11:34:44

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

我有一个Test api应用程序,使用它我在控制器中执行不同的操作。但是我无法正确实现它。

该控制器没有模型。它只是将控制权委派给某些服务。我无法将发布数据发送到操作。

class SimplexController < ApplicationController
    def initiate_request
        response = SimplexServices.new.initiate_request simplex_params
        render json: response
    end


    def update_kyc
        response = SimplexServices.new.initiate_kyc update_kyc_params
        render json: response
    end

    private 
    def simplex_params
        params.permit!
    end

    def update_kyc_params
        params.permit!
    end
end

对于 initiate_request ,我在下面的正文中有一个帖子数据

{
  "account_details": {
    "logins": [
      {
        "id": "200",
        "ip": "192.117.97.229",
        "uaid": "c387c6q6sr27csbuftfvq9v652",
        "timestamp": "2017-06-18T11:20:25Z",
        "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
        "is_api_initiated": false,
        "http_accept_language": "en-US,en;q=0.8,he;q=0.6"
      }
    ],
    "is_2fa_enabled": false,
    "partner_end_user_id": "246915",
    "identity_kyc_details": {
      "last_name": "Durden",
      "first_name": "Tyler",
      "document_id": "AB7654321"
    },
    "last_kyc_verification_timestamp": "2015-12-31T16:03:47Z"
  },
  "personal_details": {
    "email": "tylerdurden@paperstsoap.com",
    "phone": "+12885550153",
    "gender": "m",
    "address": {
      "zip": "19808",
      "city": "Bradford",
      "country": "US",
      "state": "DE",
      "address_line_1": "537 Paper street"
    },
    "last_name": "Durden",
    "first_name": "Tyler",
    "middle_name": "",
    "date_of_birth_ts": "1972-02-22T12:00:00Z"
  },
  "transaction_details": {
    "last_20_txs": [],
    "current_balance": {
      "amount": 0,
      "currency": "usd"
    },
    "payment_details": {
      "order_id": "PRTNR135791",
      "payment_id": "E98986D8-03C0-D777-07CD-C56496E2F266",
      "fiat_total_amount": {
        "amount": 1433.25,
        "currency": "usd"
      },
      "payment_flow_type": "deposit",
      "destination_wallet": {
        "address": "16M8D1ZgkWjziw8BaJDT4w1uBF4whfG7mW",
        "currency": "btc"
      }
    }
  }
}

以下 update_kyc 的发帖请求

{
  "documents": [
    {
      "document_type_id": "1",
      "document_number": "doc123"
    },
    {
      "document_type_id": "2",
      "document_number": "doc456"
    }
  ]
}

我如何允许我在控制器中使用它?

现在我使用以下代码允许所有操作。但让一切都不好。

private 
def simplex_params
  params.permit!
end

我必须如何在控制器中实现这种情况?

谢谢, 阿吉斯

1 个答案:

答案 0 :(得分:0)

首先关于许可证的使用,这取决于要求。如果相关数据不敏感,则可以跳过允许并让所有参数进行批量更新。当模型包含密码,角色等敏感数据时,许可证主要用于防止应用程序被大量分配。

当将json数据传递到Rails api时,可以在参数上使用permit,只需选择键并根据需要选择哈希值即可。从文档中:

   params = ActionController::Parameters.new({
   person: {
   name: "Francesco",
   age:  22,
   role: "admin"
     }
   })

permitted = params.require(:person).permit(:name, :age)
permitted            # => <ActionController::Parameters {"name"=>"Francesco", "age"=>22} permitted: true>

以您为例,通过JSON解析并删除不需要的密钥