简体   繁体   中英

how i can render a json for an list of ActiveRecord in Rails?

for example, i got a list of Target in my controller

@targets = @user.targets.all

then i want to render @targets as json result for client ajax call. in Target class define, i have some column i don't want to render. i know when render single @target, we can use

:only 

or

:except

but, how to deal with the list?

i tried

@targets.each do |target| xxxx end

to merge the json result, failed.

Override as_json in your Target class and specify which columns you don't want to be rendered.

More details here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM