开发者

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

开发者 https://www.devze.com 2023-03-26 20:23 出处:网络
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.

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 @ta开发者_开发技巧rget, 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消