【问题标题】:Create external customer from Devise user via HTTP request通过 HTTP 请求从 Devise 用户创建外部客户
【发布时间】:2017-05-20 22:24:04
【问题描述】:

我需要的是,在使用 Devise 创建新用户后,我的应用立即将带有该用户 ID 的 HTTP 请求 (POST) 发送到外部 API(这将在该平台上创建一个客户)。该请求将返回一个 Customer_ID,需要为用户存储在数据库中。

最好的方法是什么?

【问题讨论】:

    标签: ruby-on-rails json ruby api devise


    【解决方案1】:

    after_create 回调在这里可以派上用场。

    在您的用户模型中,

    after_create :set_customer_id
    
    private
    
    def set_customer_id
      response = API_REQUEST
      update_attribute(:customer_id, response.body[:customer_id])
    end
    

    假设,响应正文为json,所需键为customer_id

    您可以阅读 cmets 中指出的有关 before_create 和其他回调 here 的信息。

    【讨论】:

    猜你喜欢
    • 2015-02-23
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2013-04-06
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多