首页 > 解决方案 > ActionController::ParameterMissing(参数缺失或值为空:account)

问题描述

大师,需要有关 RoR API 的帮助 我看到所有必需的参数都已发送,但服务器不喜欢其中的一个或一些参数。

控制器:def create @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 开始 POST "/accounts?name=TD-Trading¤cy_id=1&institution=TD%20Waterhouse&type=Investment&client_id=1" for ::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"], ["LIMIT", 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'

标签: ruby-on-rails

解决方案


这段代码:

def account_params
  params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end

是说它需要一个包含属性的帐户哈希。您的日志显示您发送了所有未包含在帐户哈希中的属性。您可以通过将参数包装在帐户哈希中或从帐户参数中删除require(:account)部分来解决此问题。


推荐阅读