【问题标题】:RoR user add via JSONRoR 用户通过 JSON 添加
【发布时间】:2012-04-23 07:26:08
【问题描述】:

我们通过 POST 接收格式为 {email,password,hash,currentDate,companyName} 的 json-object

需要使用 RoR 方法在我们的数据库(postrege)中创建用户。

请帮助实现该方法。

【问题讨论】:

  • 粘贴您通过 POST 方法接收到的确切 JSON 数据。
  • {email:'example@example.com',password:'passexp',hash:'abc',currentDate:'23.04.2012',companyName:'Test_company'}
  • 控制器中params的值是多少?将 params.inspect 放入您的控制器操作名称并粘贴输出。
  • 我是 RoR 的新手。我现在没有控制器。只需要了解它是如何工作的。
  • 无论如何,感谢您的时间和帮助。

标签: ruby-on-rails database json post


【解决方案1】:

我建议您阅读“动作控制器概述”rails 指南的JSON/XML parameters 部分。也许你无法修改你得到的 JSON,你必须查看config.wrap_parameters 选项。

如果您收到类似的 JSON 对象

{"user" : {"email": "user@example.com",
           "password": "secret",
           "hash": "sd98ds789fsd",
           "currentDate": "the-current-date",
           "companyName": "Umbrella corp."}}

你会像往常一样在 params 哈希中找到它:

# This will be "user@example.com"
params[:user][:email]
# This will contain "secret"
params[:user][:password]

【讨论】:

  • {email:'example@example.com',password:'passexp',hash:'abc',currentDate:'23.04.2012',companyName:'Test_company'}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-28
  • 2018-04-19
  • 1970-01-01
  • 2020-05-16
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多