[英]Change ActiveModel Serializer based on controller
How can I have a certain set of controllers use a different adapter? 如何让一组特定的控制器使用其他适配器?
I think I can do something like this in a before_filter
: 我想我可以在before_filter
做这样的事情:
ActiveModel::Serializer.config.adapter = :json
Then on another set of controller's before_filter
: 然后在另一组控制器的before_filter
:
ActiveModel::Serializer.config.adapter = :json_api
The code won't be threadsafe though.. 该代码虽然不是线程安全的。
Based on the code, you can specify the adapter in your controller action like that: 根据代码,您可以在控制器操作中指定适配器,如下所示:
render json: @users, adapter: :json_api
It is of course not that convenient, as having a before_action
in the base controller, but should be threadsafe. 当然,它before_action
在基本控制器中具有before_action
那样方便,但是应该是线程安全的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.