Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to hide attributes in a Rails Jbuilder template?

I know you can explicitly list fields like so,

json.(model, :field_one, :field_two, :field_three)

But is there anything similar to the following,

json.(model, except: :field_two)

which would output all of the model fields except the one called out?

like image 477
sambecker Avatar asked Jul 11 '16 20:07

sambecker


2 Answers

Try json.merge! model.attributes.except("field_one", "field_two")

like image 113
vich Avatar answered Nov 12 '22 15:11

vich


I had done something like this. Get an array of all desired attributes of model

model.attributes.keys.map { |key| key.to_sym } - [:field_one, :field_two]

Which can be written like

 model.attributes.keys.map(&:to_sym) - [:field_one, :field_two]

Then splat the array while passing in jbuilder

json.(model, *(model.attributes.keys.map(&:to_sym) - [:field_one, :field_two]))
like image 40
Kumar Avatar answered Nov 12 '22 15:11

Kumar