我的 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/