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

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -