rails hide field in json code example
Example 1: rails hide field in json
class User < ApplicationRecord
def as_json(options={})
options[:except] ||= [:ip]
super(options)
end
end
Example 2: rails hide field in json
class User < ActiveRecord::Base
def to_json(options={})
options[:except] ||= [:ip]
super(options)
end
end
Example 3: rails hide field in json
respond_to do |format|
format.json { render :json => @user, :except=> [:ip] } # or without format block: @user.to_json(:except => :ip)
end