ruby on rails - How to change values overriding the 'to_json' method? -
i using ruby on rails 3 , override to_json method.
at time use following code in order avoid export important information.
def to_json super( :except => [ :password ] ) end if want change value using method, how can do?
for example, capitalize user name
:name => name.capitalize on retrieving this
@user.to_json
if want render :json => @user in controller rails 3, can override as_json in model:
class user < activerecord::base def as_json(options={}) result = super({ :except => :password }.merge(options)) result["user"]["name"] = name.capitalize result end end here's post differences between to_json , as_json.
Comments
Post a Comment