2016-08-02 78 views
0

渲染時,可以有條件除了,僅包含或包含選項嗎?所以,就像下面的例子:有條件的,除了,只包含在Rails的渲染json中

render json: @post, 
except: [:author] 

是否有可能有除了選項或類似的選項是有條件的?

理想情況下,有條件的做法可以讓我處理很多不同的情況和案例。

也許就像是這樣的:

render json: @post, 
except: return_excluded_keys 

return_excluded_keys函數可以返回需要排除的鍵。

我正在使用Rails 4.2.6和Active Model Serializer 0.9.3。

回答

0

可能:

render json: @post.as_json(except: [:author]) 
+0

對不起,我可能沒有關注。這是如何讓'except'部分成爲有條件的? – geoboy

+0

哎呀,我很抱歉,我誤解了你的問題。你的意思是渲染'作者'只有一些條件符合?如果是這樣,爲什麼不只是使用三元運算符'條件?渲染(json:@post):渲染(json:@post,除了:[:author])' – Canh

+0

我希望有一個有條件的方法來做到這一點,使我能夠處理許多不同的情況和案例。就像可能是這樣:'except:return_excepted_keys',該函數可能會返回需要排除的鍵。 – geoboy