I am using Ruby on Rails 3 and I would like to override (possibly in the model file) the as_json
or to_json
method in order to respond_to
an HTTP request without including some information.
In my Account model I have
def as_json(options = {})
super(
:except => [
:password
]
)
end
In my controller I have
format.json {
render :json => @account, :status => 200
}
When I make a request, for example, to /account/1.json
I have back also the password attributes that, for security reasons, I don't want.
So, how can I prevent to include specified information?
I can do this and it works
format.json {
render :json => @account.to_json(:except => [:password]), :status => 200
}
but it I need to refactor.