0

我正在使用https://github.com/rfunduk/rails-stripe-connect-example中的示例设置条带连接,并且在使用序列化存储时遇到问题stripe_account_status,应该将其存储为数组。

这就是它存储方式(从上面的示例链接生成)

在此处输入图像描述

{"details_submitted"=>false, "charges_enabled"=>true, "transfers_enabled"=>false, "fields_needed"=>["legal_entity.first_name", "legal_entity.last_name", "legal_entity.dob.day", "legal_entity.dob.month", "legal_entity.dob.year", "legal_entity.address.line1", "legal_entity.address.city", "legal_entity.address.postal_code", "bank_account"], "due_by"=>nil}

这就是我的应用程序存储它的方式

在此处输入图像描述

{:details_submitted=>false, :charges_enabled=>true, :transfers_enabled=>false, :fields_needed=>["legal_entity.first_name", "legal_entity.last_name", "legal_entity.dob.day", "legal_entity.dob.month", "legal_entity.dob.year", "legal_entity.address.line1", "legal_entity.address.city", "legal_entity.address.postal_code", "bank_account"], :due_by=>nil}

就我而言,一切都设置相同。唯一的区别是第一个示例使用

serialize :stripe_account_status, JSON

我的应用程序只有

serialize :stripe_account_status

原因是当我添加JSON时出现此错误:

JSON::ParserError - 795: unexpected token at '':

我尝试找出 JSON 错误,包括将 config/initializers/cookies_serializer.rb 更改为使用:hybrid但这给了我同样的错误。

有人可以指出我修复 JSON 问题找到一种方法来确保stripe_account_status正确存储为数组的正确方向。

以下是用于存储数组的方法:

if @account
  user.update_attributes(
    currency: @account.default_currency,
    stripe_account_type: 'managed',
    stripe_user_id: @account.id,
    secret_key: @account.keys.secret,
    publishable_key: @account.keys.publishable,
    stripe_account_status: account_status
  )
end

def account_status
{
  details_submitted: account.details_submitted,
  charges_enabled: account.charges_enabled,
  transfers_enabled: account.transfers_enabled,
  fields_needed: account.verification.fields_needed,
  due_by: account.verification.due_by
}
end

谢谢我真的很感激你能指出我的任何方向!

4

1 回答 1

1

当您要求 Rails 序列化模型上的属性时,它会默认将对象存储为 YAML 字符串。

您可以要求 Rails 以不同的方式进行序列化,正如您通过提供一个类来进行序列化所注意到的那样,例如

serialize :stripe_account_status, JSON

添加它时它不起作用的原因是因为您可能已经使用 YAML 在数据库中拥有一条记录,因此 Rails 在从数据库读取时无法将其解析为有效的 JSON 字符串。如果只是不需要的开发数据,可以删除记录再使用JSON,否则需要将当前的YAML字符串转成JSON。

在解析数据库中的序列化字符串时,Rails 还将符号化哈希的键。这是您问题中哈希值之间的唯一区别,在实践中应该无关紧要。如果您出于某种原因需要字符串键,您可以使用#stringify_keysRails 提供的哈希方法。

于 2015-06-01T16:19:45.183 回答