Say i have this short code:
item = Item.find(params[:id]) render :json => item.to_json
but i needed to insert/push extra information to the returned json object, how do i do that?
Lets say i need to insert this extra info:
message : "it works"
Thanks.
item = Item.find(params[:id]) item["message"] = "it works" render :json => item.to_json
The to_json
method takes an option object as parameter . So what you can do is make a method in your item class called as message and have it return the text that you want as its value .
class Item < ActiveRecord::Base def message "it works" end end render :json => item.to_json(:methods => :message)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With