[:id, :name])} 但我还-6ren">
gpt4 book ai didi

ruby-on-rails - 向 ActiveRecord 序列化添加更多属性?

转载 作者:数据小太阳 更新时间:2023-10-29 07:16:35 25 4
gpt4 key购买 nike

我的 json 序列化工作正常

render :json => "#{current_object.serialize(:json, :attributes => [:id, :name])}

但我还想在将数据设置回客户端之前向 json 添加更多数据。主要是 auth_token。

疯狂地用 Google 搜索,但我找不到序列化将采用什么选项来允许我将我的其他数据附加/合并到 JSON 中。

希望找到这样的东西......

current_object.serialize(:json, :attriubtes => [:id, name], :magic_option => {:form_authenticity_token => "#{form_authenticity_token}"})

最佳答案

您需要 :methods 键,它的工作方式类似于 :attributes,但将包含给定方法的结果。在你的情况下:

current_object.to_json(
:attributes => [:id, :name],
:methods => [:form_authenticity_token]
)

关于ruby-on-rails - 向 ActiveRecord 序列化添加更多属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/683191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com