Rails 集合变 json 不调用元素的 to_json

birbird · September 20, 2013 · Last by birbird replied at September 20, 2013 · 1929 hits

我有一个 Place 类,我重写了他的 to_json,是能用的 format.json { render :json => @place } 能得到正确结果。

但是,这样就不对 @places = Place.all format.json { render :json => @places } 这样根本不会调用 Place.to_json。

请问这是咋回事,知道请指点,谢了先!

你是想说

format.json { render :json => Place }

和这个

@places = Place.all
format.json { render :json => @places }

不一样吗?不然你的第一段代码和第二段代码。。。可能一样吗?

我猜得重写 as_json

@places.collect(&:as_jason)

You need to Sign in before reply, if you don't have an account, please Sign up first.