【问题标题】:json generated with rabl includes a wrapped_string attribute用 rabl 生成的 json 包含一个 Wrapped_string 属性
【发布时间】:2013-01-29 21:33:59
【问题描述】:

我的模特:

def full_name
  "#{first_name} #{middle_name} #{last_name} #{suffix_name}".mb_chars.upcase.squeeze(' ').strip
end

我的 rabl 模板:

collection @users
attributes :id, :full_name

这是插入一个名为wrapped_string的额外孩子

[{"user":{"id":73,"full_name":{"wrapped_string":"FOO BAR"}}}]

但我需要类似的东西:

[{"user":{"id":73,"full_name":"FOO BAR"}}]

如果我不使用mb_chars 效果很好,但我必须使用它。我该怎么办?

【问题讨论】:

  • 听起来很蠢,但你有没有试过在脱衣舞后面加to_s
  • @MrYoshiji 哈哈,这很愚蠢。感谢您的提示;)

标签: ruby-on-rails json rabl


【解决方案1】:

您需要添加to_s 再次将 MultiByte::Chars 转换为字符串:

def full_name
  "#{first_name} #{middle_name} #{last_name} #{suffix_name}".mb_chars.upcase.squeeze(' ').strip.to_s
end

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-20
    相关资源
    最近更新 更多