【问题标题】:ActionController::ParameterMissing (param is missing or the value is empty: account)ActionController::ParameterMissing(参数缺失或值为空:account)
【发布时间】: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&currency_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


【解决方案1】:

这段代码:

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

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

【讨论】:

  • 非常感谢,我正在使用邮递员测试api并使用参数,但是当我使用正文发送数据时,它解决了问题。所以邮递员在将数据作为哈希发送时收到了适当的响应,再次感谢您的解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 2021-11-20
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
相关资源
最近更新 更多