0

我正在设置条带连接与从https://github.com/rfunduk/rails-stripe-connect-example的例子,我遇到了一个问题,使用序列化来存储stripe_account_status应存储为一个数组。Rails序列化没有正确存储

这是怎么回事应该存储(从上面的例子链接生成)

enter image description here

{"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} 

这是怎么我的应用程序收藏起来

enter image description here

{: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 

谢谢,我真的很感激任何方向,你可以点我!

回答

1

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

你可以问Rails的序列化不同,因为你已经提供一类做系列化如

serialize :stripe_account_status, JSON

为什么当你添加这种不正常的原因,发现是因为你想必在使用YAML的数据库中已经有记录,因此从数据库读取数据时,Rails无法将其解析为有效的JSON字符串。如果只是您不需要的开发数据,您可以删除记录然后使用JSON,否则您需要将当前的YAML字符串转换为JSON。

在解析数据库中的序列化字符串时,Rails也将象征着散列的键。这是你问题中哈希之间的唯一区别,在实践中应该不重要。如果你出于某种原因需要String键,你可以在Rails提供的哈希上使用#stringify_keys方法。