This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the rails category.
Last Updated: 2024-11-21
I had the following
def update_header
{
item_count: @order.line_items.count,
total: current_order.display_total
}.to_json
end
In my JavaScript code, I assumed the total
key would be something like $30
,
since that is how current_order.display_total
displayed in rspec
or irb
output
In fact, it turned into a hash in this context: {amount: 30, currency: "$"}
,
which the JavaScript client-code was totally unprepared to handle.
Do not assume anything about the shape of a complex object which is called
within a to_json
block. The to_s
representation in particular should not be
expected.