ruby-on-rails - 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'
解决方案
这段代码:
def account_params
params.require(:account).permit(:client_id, :currency_id, :name, :type, :institution)
end
是说它需要一个包含属性的帐户哈希。您的日志显示您发送了所有未包含在帐户哈希中的属性。您可以通过将参数包装在帐户哈希中或从帐户参数中删除require(:account)
部分来解决此问题。
推荐阅读
- xml - 父级属性的 XPath 不包含特定字符?
- netlogo - 如何从社区打开一个netlogo模型?
- scala - 是否可以在 Scala Trait 中声明静态变量和函数?
- office-addins - 这是一个无效的 xsi:type 'http://schemas.microsoft.com/office/taskpaneappversionoverrides:Project'
- python - python线程下载字典列表
- python - 如何在 python 中安装 gensim 和运行包?
- mysql - COUNT 和 COUNT,GROUP BY 第一个计数
- android - 如何使用 Android Facebook SDK 将照片从内部存储共享到故事?
- javascript - 在弹出窗口中成功登录 Paypal 后将重定向 url 参数传递回主窗口
- c# - 为什么返回的是List
?