【发布时间】:2020-11-27 08:28:03
【问题描述】:
大师们, 需要有关 RoR API 的帮助 我看到正在发送所有必需的参数,但是服务器不喜欢其中的一个或一些参数。
控制器: 定义创建 @account = Account.new(account_params)
if @account.save
render json: @account, status: :created, location: @account
else
render json: @account.errors, status: :unprocessable_entity
end
结束
def account_params
params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end
调用如下所示: 已于 2020-11-27 01:16:08 -0700 为 ::1 开始 POST "/accounts?name=TD-Trading¤cy_id=1&institution=TD%20Waterhouse&type=Investment&client_id=1" AccountsController#create as / 处理 参数:{"name"=>"TD-Trading", "currency_id"=>"1", "institution"=>"TD Waterhouse", "type"=>"Investment", "client_id"=>"1" } 客户端负载 (0.4ms) SELECT "clients".* FROM "clients" WHERE "clients"."email" = $1 ORDER BY "clients"."id" ASC LIMIT $2 [["email", "akaghzi@gmail.com "], ["限制", 1]] 在 271 毫秒内完成 400 个错误请求(ActiveRecord:0.4 毫秒 | 分配:1706)
ActionController::ParameterMissing(参数缺失或值为空:account):
app/controllers/accounts_controller.rb:55:in account_params' app/controllers/accounts_controller.rb:21:in create'
【问题讨论】:
-
模型出现问题,我将列命名为“type”,我现在已将其更改为“account_type”,但行为相同。
-
我已经使用 rails 控制台测试了这些值并且它有效:account = Account.new(name: 'TD-Trading', authority: 'TD Waterhouse', account_type: 'Investment', currency_id: 1 ,client_id:1)account.valid? => 是的
标签: ruby-on-rails