简体   繁体   中英

How can I assign the results of a Rails “render json:” and “render_with” to a variable, for storage in a table?

I have some code rendering json as follows...

        serialized = ActiveModel::ArraySerializer.new(actions, each_serializer: Api::V1::ActionSerializer)
        render json: { actions: serialized }

What I need... is to turn serialized into a string so I can store it in a database record. serialized.to_s does not work.

How can I turn this #<ActiveModel::ArraySerializer:0x007fdfddc332b8> into a string representation of the JSON?

您可以使用ActiveSupport :: JSON#encode来获取JSON的字符串表示形式。

ActiveSupport::JSON.encode(serialized)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM